首页 > 解决方案 > How to keep a cell active (not just selected/highlighted) when you refresh a DataGridView?

问题描述

I have a DataGridView with a list of firewall rules. I have a button to update/reload the DataGridView, but I want to keep the last cell I selected active (and visible) when I reload.

With the code I currently am using it only keeps it highlighted, but not exactly active/usable. Here is the snippet:

int x = DataGridView1.CurrentRow.Index;
updateTable();
DataGridView1.Rows[x].Cells[0].Selected = true;

And here's the updateTable() code:

public void updateTable()
        {
            DataGridView1.DataSource = null;
            DataGridView1.Rows.Clear();
            DataTable dt = new DataTable();
            dt = myDLL.getFirewallRules();
            DataGridView1.DataSource = dtFirewall;
            DataGridView1.Columns["OriginalIndex"].Visible = false;
        }

(being that the ["Original Index"] column represents a fixed index I want hidden.)

I've found the code on how to keep it as the first in the scroll list (if that makes sense), but can't make it selected and ready to use, it always defaults to the first cell of the first row.

How can I do this?

标签: c#.netwinformsdatagridview

解决方案


我相信您可以使用 CurrentCell 属性来完成您想要完成的工作。

这是一个已经解决的类似问题。 Datagridview:如何在编辑模式下设置单元格?

如果您对 BeginEdit 功能有疑问,请尝试:

dataGridView1.EditMode = DataGridViewEditMode.EditOnKeystrokeOrF2;

我希望这会有所帮助。


推荐阅读