如何以编程方式单击DataGridView的单元格? [英] How do I click on a Cell of a DataGridView programmatically?

查看:59
本文介绍了如何以编程方式单击DataGridView的单元格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个datagridview_cellclick方法,当我单击一个单元格时,该方法会将dataGridView中的所有内容放入变量中。
所以那里没问题。

I have a method datagridview_cellclick who does put everything from the dataGridView into variables when I click on a cell. So no problem there.

但是我想在我放下程序时自动单击同一DataGridView的第一个单元格。不是自己用鼠标单击,而是像程序在创建带有dataGridView的表单时在第一个单元格上单击一样。

But i want to automatically click on the first cell of the same DataGridView when I lauch my program. Not click by myself with the mouse, but like the program does click on the first cell itself when the form with the dataGridView is created.

然后单击将调用该方法上面的代码,即使我没有单击第一个单元格,也可以设置变量。
我不确定是否明确。
我该怎么做?

And this click would call the method above so I get the variables set even if I don't click on the first cell. I'm not sure if this clear. How can I do this?

推荐答案

假定使用Windows窗体

Assuming that Windows Forms are used

Pretty Naive方法是调用具有所需参数集的函数:

Pretty Naive method is to call the function with required parameter's set:

dataGridView1_CellClick(this.dataGridView1, new DataGridViewCellEventArgs(0, 0));

尽管我不确定您在CellClickEvent中正在做什么,因为这会改变您想要的方式访问该单元格(或对该单元格执行某些操作)。为了完整起见,我做了什么:

Although I am not sure what you are doing in the CellClickEvent, because that can change the way you want to access the cell (or do something with that cell). For completeness sake what i did:

  public Form1()
    {
        InitializeComponent();
        dataGridView1.CurrentCell = dataGridView1.Rows[0].Cells[0];
        dataGridView1_CellClick(this.dataGridView1, new DataGridViewCellEventArgs(0, 0));
    }

    private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
    {
        MessageBox.Show("" + e.ColumnIndex + e.RowIndex);
    }

这篇关于如何以编程方式单击DataGridView的单元格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆