c# - 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?
解决方案
我相信您可以使用 CurrentCell 属性来完成您想要完成的工作。
这是一个已经解决的类似问题。 Datagridview:如何在编辑模式下设置单元格?
如果您对 BeginEdit 功能有疑问,请尝试:
dataGridView1.EditMode = DataGridViewEditMode.EditOnKeystrokeOrF2;
我希望这会有所帮助。
推荐阅读
- angular - 带有 ng-content -> form.valid/dirty 等的 Angular 表单不起作用
- apache-spark - Spark 应用程序如何访问 Amazon S3 存储桶中的文件/目录?
- protractor - 量角器没有默认建议 browser.actions() 并抛出 TypeError: browser.actions(...).dragAndDrop(...).perfrom is not a function
- python - 如何访问类中另一个函数中的变量?
- sql - 如何加入 2 个子查询
- wolfram-mathematica - 如何解决这个积分?
- asp.net - GridView ASP 中的图像
- c - C Low level I/O : 为什么它会挂在 while 循环中?
- python - 使用来自 KAFKA 主题的数据并从中提取字段并使用 python 存储在 MySQL 中
- r - 在 R 中,如何根据列表自动绘图?