datagridview动态添加数据后,如何保证选中的单元格还在刷新之前的位置?因为添加了数据,所以刷新之前选中的单元格的index在刷新之后就改变了,每次刷新添加的数据的行数是未知的。
如果还有个唯一值的列(不一定要显示的),例如学号。在更新前记录学号,。更新后 获取对应学号的行 idex。
没有就比较整个行的值。如果有完全重复的行,那就选第一个相同的吧。。。。
然后在代码里选定。
public Form1()
{
InitializeComponent();
this.Load += Form1_Load;
//单元格选中
this.dataGridView1.SelectionMode = DataGridViewSelectionMode.CellSelect;
//单选
this.dataGridView1.MultiSelect = false;
this.dataGridView1.Rows.Add();
this.dataGridView1.Rows.Add();
}
private void Form1_Load(object sender, EventArgs e)
{
//手动选中
this.dataGridView1.Rows[1].Cells[1].Selected = true;
//throw new NotImplementedException();
}