如何处理Datagridview的“按钮”列中的单击事件?

我正在使用C#开发Windows应用程序。我DataGridView用来显示数据。我在其中添加了一个按钮列。我想知道如何处理DataGridView中该按钮上的click事件。



HUH函数
浏览 1547回答 3
3回答

收到一只叮咚

对于WinForms,这里已完全回答:DataGridViewButtonColumn类而在这里:如何:响应GridView控件中的按钮事件取决于您实际使用的控件。(您的问题是说DataGrid,但是您正在开发Windows应用程序,因此要使用的控件中有一个DataGridView ...)

沧海一幻觉

这是更好的答案:您不能为DataGridViewButtonColumn中的按钮单元实现按钮单击事件。相反,您可以使用DataGridView的CellClicked事件并确定是否为DataGridViewButtonColumn中的某个单元触发了该事件。使用事件的DataGridViewCellEventArgs.RowIndex属性可以找到单击的行。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);  }}
打开App,查看更多内容
随时随地看视频慕课网APP