简体   繁体   English

以编程方式选择一行DataGridView

[英]Selecting a row of DataGridView programmatically

In my form application, there is a ( buttonNEW ) that selects NewIndexRow of DataGridView and I want to change index of datagridview with this button. 在我的表单应用程序中,有一个( buttonNEW )选择DataGridView NewIndexRow ,我想使用此按钮更改datagridview的索引。

private void buttonNew_Click(object sender, EventArgs e)
    {
        if (dataGridView.CurrentRow.Index!=dataGridView.NewRowIndex)
        {
            dataGridView.ClearSelection();
            dataGridView.Rows[dataGridView.NewRowIndex].Selected = true;
            label1.Text = dataGridView.CurrentRow.Index.ToString();

        }
    }

But after clicking the button the index of DataGridView does not change. 但是在单击按钮后, DataGridView的索引不会更改。 What is the problem? 问题是什么?

This should work :- 这应该工作:

int numofRows = dataGridView.rows.count;

dataGridView.CurrentCell = dataGridView.Rows[numofRows - 1].Cells[0];

Or I think you could also do this :- 或者我认为您也可以这样做:

dataGridView.CurrentCell = dataGridView.Rows[dataGridView.NewRowIndex].Cells[0];

Try with Linc: 尝试使用Linc:

private void buttonNew_Click(object sender, EventArgs e)
    { 

     dataGridView.Rows.OfType<DataGridViewRow>().Last().Selected = true;

   }

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

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