2011-10-27 41 views
0

嗨,我有一個Windows窗體應用程序,我有一個最後一列的數據網格按鈕列。我已經讀過,要能夠回答點擊事件,我必須使用「cellClick」事件處理程序,但是當我使用這個事件觸發器時,無論是單擊的按鈕還是單元格在角落單擊。那麼有沒有更好的方法來解決這個問題,以及如何。感謝您的幫助提前。將動作添加到datagrid列按鈕的最佳方式是什麼?

+0

您使用的DataGridView和DataGridViewButtonColumn什麼樣的? http://stackoverflow.com/questions/6085930/add-button-column-in-a-databound-datagridview –

+0

是的,我使用dataGridView和DataGridViewButtonColumn。 – Jordan

回答

1

我假設你正在使用數據網格視圖,則此解決方案

這是你需要拍攝按鈕點擊事件DataGridView的處理程序。

this.dgvList.CellContentClick += new DataGridViewCellEventHandler(DGV_CellContentClick); 

這這個問題是按鈕單擊處理例如

public void DGV_CellContentClick(object sender, DataGridViewCellEventArgs e) 
{ 
    int selectedRowIndex = int.Parse(e.RowIndex.ToString()); 

    if (this.dgvList.Columns[e.ColumnIndex] == buttonColumn && selectedRowIndex >= 0) 
    { 
     //do what ever you want 
     // DataRow dr = DataGridViewHelper.GetDataRow(this.dgvList); 
     //MessageBox.Show((string)dr["FirstName"]); 
    } 
} 

我希望它會幫助你....

相關問題