C# 在datagrid中设置单元格和行指针无效

C# 在datagrid中设置单元格和行指针无效,c#,winforms,datagrid,C#,Winforms,Datagrid,我有一个带有datagrid的表单,其中包含表中的记录列表。当用户选择一条记录来编辑其值时,我希望返回到datagrid中的同一行(记录),并将其显示为选中状态。当然,datagrid应该显示新的(编辑的)值。 EditButton中的代码完成了所有操作,但结果不是预期的 private void zButtonEdit1_Click(object sender, EventArgs e) { // Store the rowIndex and columnInd

我有一个带有
datagrid
的表单,其中包含表中的记录列表。当用户选择一条记录来编辑其值时,我希望返回到datagrid中的同一行(记录),并将其显示为选中状态。当然,datagrid应该显示新的(编辑的)值。
EditButton
中的代码完成了所有操作,但结果不是预期的

    private void zButtonEdit1_Click(object sender, EventArgs e)
    {
        // Store the rowIndex and columnIndex values
        zButtonEdit1.rowIndex = dataGridView1.CurrentRow.Index;
        zButtonEdit1.cellIndex = dataGridView1.CurrentCell.ColumnIndex;
        // Store the Id of the current record
        zButtonEdit1._id_ = Convert.ToInt16( dataGridView1["id_lice", zButtonEdit1.rowIndex].Value.ToString());
        // Open the Editform with required data for that ID 
        Lice frmLice = new Lice(zButtonEdit1.UIB, zButtonEdit1._id_);
        frmLice.ShowDialog();
        // When editing is finished, refresh the grid with new values
        refresh_controls();
        //Set the current record pointer (or row pointer) to the record that was edited
        this.dataGridView1.Rows[zButtonEdit1.rowIndex].Cells[zButtonEdit1.cellIndex].Selected = true;
    }
这就是我得到的 很明显,我只更改了选择,但不知道如何更改
(记录)指针。

按要求:

要设置当前选定的单元格,请执行以下操作:

this.dataGridView1.CurrentCell = this.dataGridView1[YourColumn,YourRow];
信息:

您正在单元格上设置Selected=true,若要在行上设置它,请在行上调用它:
this.dataGridView1.Rows[zButtonEdit1.rowIndex].Selected=true
Reference:我先尝试过,但结果类似,只是现在选择的行不是单元格,但记录指针没有改变。如果你喜欢或需要的话,我可以用它发布图片。啊,对不起,我误解了,我想这就是你想要的:
dataGridView1.CurrentCell=this.dataGridView1[YourColumn,YourRow]信息:谢谢你的工作!我怎样才能给你的答案投票?在评论中没有选项。@SilentStorm为什么不把这个解决方案写下来作为答案,这样Zed McJack就可以接受它?!