简体   繁体   English

如何以编程方式单击DataGridView的单元格?

[英]How do I click on a Cell of a DataGridView programmatically?

I have a method datagridview_cellclick who does put everything from the dataGridView into variables when I click on a cell. 我有一个datagridview_cellclick方法,当我单击一个单元格时,它会将dataGridView中的所有内容都放入变量中。 So no problem there. 所以那没问题。

But i want to automatically click on the first cell of the same DataGridView when I lauch my program. 但是我在放样时想自动单击同一DataGridView的第一个单元格。 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. 不是自己用鼠标单击,而是像程序在创建带有dataGridView的表单时单击第一个单元格本身。

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? 我怎样才能做到这一点?

Assuming that Windows Forms are used 假设使用Windows窗体

Pretty Naive method is to call the function with required parameter's set: 漂亮的天真方法是调用具有所需参数集的函数:

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

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). 尽管我不确定您在CellClickEvent中正在做什么,因为这会改变您想要访问单元格的方式(或对该单元格执行某些操作)。 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);
    }

Refactor your logic in datagridview_cellclick to a separate function, then use DataGridView.CurrentCell to set cell focus. datagridview_cellclick中的逻辑重构为一个单独的函数,然后使用DataGridView.CurrentCell设置单元格焦点。 You may set it in the Form_Load 您可以在Form_Load设置

if (DataGridView.Rows.Count >= 0)
{
    // Set the first cell
    DataGridView.CurrentCell = DataGridView.Rows(0).Cells(0);
    // Cell logic
    UserSelectedCell(DataGridView.CurrentCell);
}

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

相关问题 如何在DataGridView单元格单击事件中获取先前选择的单元格 - How do I get the previously selected cell in a DataGridView cell click event 如何以编程方式将单元格添加到DataGridView? - How to add a Cell programmatically to a DataGridView? 如何将DataGridView中的单元格转换为按钮? - How do I convert a cell in the DataGridView into a button? 如何在DataGridView中搜索特定的单元格? - How do I search for a specific cell in a DataGridView? 如何以编程方式从datagridview中的一个单元移动到另一个? - How can I programmatically move from one cell in a datagridview to another? 如何按单元格颜色对datagridview进行排序? - how do i sort datagridview by cell color? 如何基于DataGridView中的单元格单击处理C#中的事件? - How do I handle an event in C# based on a cell click in a DataGridView? 我如何将datagridview单元格单击事件转换为DevExpress网格控件 - how do i convert datagridview cell click event to DevExpress Grid Control ComboBox 以编程方式添加到 DataGridView 单元格中,单击单元格时未展开 - ComboBox added programmatically to DataGridView cell not expanding on cell click 如何在DataGridView中以编程方式设置单元格值? - How to programmatically set cell value in DataGridView?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM