2

在我的表单应用程序中,有一个 ( buttonNEW) 选择NewIndexRowDataGridView我想使用此按钮更改 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();

        }
    }

但是单击按钮后,索引DataGridView不会改变。问题是什么?

4

2 回答 2

4

这应该工作: -

int numofRows = dataGridView.rows.count;

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

或者我认为你也可以这样做:-

dataGridView.CurrentCell = dataGridView.Rows[dataGridView.NewRowIndex].Cells[0];
于 2012-11-20T08:08:02.683 回答
1

尝试使用 Linc:

private void buttonNew_Click(object sender, EventArgs e)
    { 

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

   }
于 2012-11-20T07:36:58.897 回答