2010-11-23 58 views
1

我正在使用Visual Studio,並嘗試從用戶雙擊DataGridView單元格時獲取信息。我基本上設置CellDoubleClick事件就像任何其他Click事件,但似乎並不奏效。Visual C# - 使用CellDoubleClick事件關聯事件處理程序

代碼:

Form1.cs的

private void dataGridView1_CellDoubleClick(Object sender, DataGridViewCellEventArgs e) 
    { 

     System.Text.StringBuilder messageBoxCS = new System.Text.StringBuilder(); 
     messageBoxCS.AppendFormat("{0} = {1}", "ColumnIndex", e.ColumnIndex); 
     messageBoxCS.AppendLine(); 
     messageBoxCS.AppendFormat("{0} = {1}", "RowIndex", e.RowIndex); 
     messageBoxCS.AppendLine(); 
     MessageBox.Show(messageBoxCS.ToString(), "CellDoubleClick Event"); 
    } 

在Form1.Designer.cs相關代碼

this.dataGridView1.CellDoubleClick += new System.EventHandler(this.dataGridView1_CellDoubleClick); 

我得到的Form1.Designer代碼一個錯誤,指出,「 'dataGridView1_CellDoubleClick'沒有過載匹配代表'System.EventHandler'。

H我可以得到雙擊正確工作嗎? 謝謝。

回答

2

CellDoubleClick事件是DataGridViewCellEventHandler,而不是 EventHandler`。

您應該使用設計器添加事件句柄,該設計器將自動使用正確的委託類型。
您不應該手動編輯設計器生成的代碼。

一般來說,添加事件處理函數時,不應該明確地創建委託。
相反,你可以寫

myGrid.CellDoubleClick += MyGrid_CellDoubleClick; 
+0

好吧,我不是在Visual Studio的非常好,我是那種在我束手無策試圖讓雙擊工作。看完你的評論後,我仔細看了一眼設計師,發現了事件按鈕。現在它可以工作。謝謝。 – Rupert 2010-11-23 02:51:23