2011-10-27 37 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"]); 
    } 
} 

我希望它会帮助你....

相关问题