2016-11-23 23 views
0

在我的DataGridView中,我有一個DataGridViewLinkCell,我想使用標籤將任意字符串傳遞給DGV的事件處理程序。C#無法將綁定DataGridView控件的標記傳遞給另一個方法

這是我的LinkCell代碼:

DataGridViewLinkCell searchWU = new DataGridViewLinkCell(); 
searchWU.Value = "Check"; 
searchWU.LinkVisited = false; 
searchWU.Tag = "myTag"; 
this.hostMgmtDataGridView[colHostViewWUAvil, this.hostMgmtDataGridView.RowCount-2] = searchWU; 

然後我對DGV的事件處理程序代碼:

public void hostMgmtDataGridView_CellContentClick(object sender, DataGridViewCellEventArgs e) 
{ 
DataGridViewLinkCell derp = (DataGridViewLinkCell)sender; 
debugLabel1.Text = derp.Tag.ToString(); 
} 

DataGridViewLinkCell derp = (DataGridViewLinkCell)sender;產生以下錯誤:

Unable to cast object of type 'System.Windows.Forms.DataGridView' to type 'System.Windows.Forms.DataGridViewLinkCell'.

如果我將其更改爲DataGridView derp = (DataGridView)sender;我得到一個NULL參考與Object reference not set to an instance of an object.

我明白這些錯誤,但我不知道如何從相關的DataGridView梳理出LinkCell。

任何幫助將不勝感激 - 謝謝!

回答

0

object sender是控制,而不是選定單元格,你可以找到將細胞與DataGridViewCellEventArgsRowIndexColumnIndex性能

試試這個(未經測試)

private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e) 
    { 
     debugLabel1.Text = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Tag.ToString(); 
    } 
+0

,完美的工作!謝謝! –

相關問題