简体   繁体   English

以编程方式单击DataGridView C#中的行标题

[英]Programmatically Click Row Header in DataGridView C#

I am searching everywhere for code to programmatically Click a specific DataGridView row header, after processing a certain code sequence. 我正在到处搜索代码,以在处理了某些代码序列之后以编程方式单击特定的DataGridView行标题。

Everything I have found allows to select or highlight a row, column or cell; 我发现的所有内容都可以选择或突出显示行,列或单元格; but nothing to date Clicks the Row Header ie. 但到目前为止没有任何信息单击行标题,即。 fires the 发射

private void dgvMyGrid_RowHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e) {}

Method. 方法。

Any help on this would be greatly appreciated. 任何帮助,将不胜感激。

The solution depends on what exactly you want to achieve. 解决方案取决于您要实现的目标。

To simply make the code in the dgvMyGrid_RowHeaderMouseClick run in the simplest way, as Tzah Mama wrote, it is enough to write: 正如Tzah Mama所写,仅以最简单的方式使dgvMyGrid_RowHeaderMouseClick中的代码运行,就足以编写:

dgvMyGrid_RowHeaderMouseClick(null, null);   

Note 1: It is important to add a coment to the event code to document that it can be called in this special and not normal way. 注1:重要的是在事件代码中添加一条注释,以记录可以通过这种特殊而不正常的方式调用它。 Especially since this doesn't provide the parameters the code might expect. 特别是因为它不提供代码可能期望的参数。

Note 2: If your code needs some of the parameters you can try to provide them: 注意2:如果您的代码需要一些参数,则可以尝试提供它们:

  • It is trivial to provide the sender simply as dgvMyGrid 简单地将sender提供为dgvMyGrid是微不足道的
  • If you are not coding a DataGridView instance but an inherited class you replace the dvg name simply with this 如果您不是在编码DataGridView实例,而是在编写继承的类,则只需使用this替换dvg名称

  • The second parameter is complex and not all its parts can be easily provided, but probably are not needed either. 第二个参数很复杂,并非所有部分都可以轻松提供,但也可能不需要。

Here is the basic code to create it: 这是创建它的基本代码:

int row = ??    // your target row
int col= -1;    // no column
int x = 0;      // no offsets..
int y = 0;      // ..
int delta = 0; // no mouse wheel

DataGridViewCellMouseEventArgs E = new DataGridViewCellMouseEventArgs(col, row, x, y,
                          new MouseEventArgs(MouseButtons.Left, clicks, x, y, delta));

Note 3: Sometimes it is important to know whether an event has been called by a real user action or by code. 注3:有时重要的是要知道事件是由真实的用户操作还是由代码调用的。 Leaving a paremter null is a simple way to signal this. 将参数保留为空是发出信号的一种简单方法。 Again: Key is documentation ! 再次:关键是文档

But maybe you need the effect of the rowHeader click? 但是也许您需要单击rowHeader的效果 Which may be a row select, depending on the SelectionMode. 根据SelectionMode,可能是行选择。 This will not work by calling the script! 调用脚本将无法正常工作! Instead you must code the select as well: 相反,您还必须对select进行编码:

 DGV1.Rows[row ].Selected = true;

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

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