【问题标题】:Selecting a row of DataGridView programmatically以编程方式选择一行 DataGridView
【发布时间】:2012-11-20 07:25:24
【问题描述】:

在我的表单应用程序中,有一个 (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 的索引并没有改变。 有什么问题?

【问题讨论】:

  • 你确定 NewRowIndex 有值吗?
  • NewRowIndex 是空行。我想用户点击 buttonNEW 并选择最后一行输入数据

标签: c# winforms datagridview


【解决方案1】:

这应该可行:-

int numofRows = dataGridView.rows.count;

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

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

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

【讨论】:

  • 我试过你的方法,对我来说效果很好,效果完全一样。
【解决方案2】:

尝试使用 Linc:

private void buttonNew_Click(object sender, EventArgs e)
    { 

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

   }

【讨论】:

    猜你喜欢
    • 2011-09-10
    • 1970-01-01
    • 2011-10-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多