button click event in datagridview

后端 未结 2 1847
感情败类
感情败类 2021-01-13 09:17

I am having a button cell in datagridview.When that button is clicked,another datagridview should be visible .For every button click in the button column,the data in new dat

相关标签:
2条回答
  • 2021-01-13 10:14

    You can't implement a button clicked event for button cells in a DataGridViewButtonColumn. Instead, you use the DataGridView's CellClicked event and determine if the event fired for a cell in your DataGridViewButtonColumn. Use the event's DataGridViewCellEventArgs.RowIndex property to find out which row was clicked.

    private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e) {
        // Ignore clicks that are not in our 
        if (e.ColumnIndex == dataGridView1.Columns["MyButtonColumn"].Index && e.RowIndex >= 0) {
            Console.WriteLine("Button on row {0} clicked", e.RowIndex);
        }
    }
    

    The MSDN documentation on the DataGridViewButtonColumn class has a more complete example.

    0 讨论(0)
  • 2021-01-13 10:16

    use dataGridView1_CellContentClick instead of dataGridView1_CellClick

    private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
    {
    
        if (e.ColumnIndex == 8) //make sure button index here
        {
              //write your code here
        }
    
    }
    
    0 讨论(0)
提交回复
热议问题