简体   繁体   English

在DataGridView C#Windows窗体中单击时获取单元格的位置

[英]Getting the location of cell when clicked in DataGridView C# windows form

I have a DataGrid in a Windows Form application and I wanted to get the location of the cell the user clicks in and also display the content of the row. 我在Windows Form应用程序中有一个DataGrid ,我想获取用户单击的单元格的位置,并显示该行的内容。

I have 4 columns and the number of rows vary. 我有4列,行数各不相同。

Can someone point me in the right direction please? 有人可以指出我正确的方向吗?

Cheers 干杯

http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.cellclick.aspx http://msdn.microsoft.com/zh-cn/library/system.windows.forms.datagridview.cellclick.aspx

You will want to leverage the CellClick event to handle this. 您将需要利用CellClick事件来处理此事件。

Hook the event handler method (one similar to that in the article above) to your datagrid and you have all the information you need in the args passed to the method ( e.RowIndex for example). 将事件处理程序方法(与上一篇文章中的方法类似) e.RowIndex到您的数据网格,然后在传递给该方法的args中获得所需的所有信息(例如e.RowIndex )。

Creating the Event Handler 创建事件处理程序

private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
  //Do what you want here with e.RowIndex or e.ColumnIndex, for example
}

Hooking It Up 挂钩

this.dataGridView1.CellClick += new System.Windows.Forms.DataGridViewCellEventHandler(this.dataGridView1_CellClick);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM