C# 保持用户单击的datagridview行处于选中状态
我需要C#中的代码片段,以便在双击该行后从C# 保持用户单击的datagridview行处于选中状态,c#,.net,winforms,C#,.net,Winforms,我需要C#中的代码片段,以便在双击该行后从DataGridView中选择该行 现在我正在显示数据集中的数据,选择模式是FullRowSelect。 有什么办法可以解决这个问题吗 有两种情况需要处理: 每次计时器勾选选定行时,总是转到datagridview的第一行 单击某一行后,该行将被选中,但在计时器计时后,选中的行将转到第一行 谢谢你的帮助 新手程序员 现在我正在显示数据集中的数据,选择模式是FullRowSelect。有什么办法可以解决这个问题吗 属性将通过枚举为您执行此操作 dataGr
DataGridView
中选择该行
现在我正在显示数据集中的数据,选择模式是FullRowSelect
。
有什么办法可以解决这个问题吗
有两种情况需要处理:
FullRowSelect
。有什么办法可以解决这个问题吗
属性将通过枚举为您执行此操作
dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
至于你问题的其余部分,我认为还需要进一步的细节。你想要什么样的行为
编辑#1
根据你的评论:
在我单击一行后,一个新表单被打开。问题是,每次启用计时器时,都会调用populate_DatagridView方法,并且选中的行位于第一行,而不是保持选中并单击该行 一种解决方案可以是:
private _dataGridViewRowSelectedIndex;
private void dataGridview1_CellDoubleClick(object sender, DataGridViewCellEventArgs e) {
DataGridView dgv = (DataGridview)sender;
if (dgv.Rows.GetRowState(e.RowIndex) == DataGridViewElementStates.Selected)
_dataGridViewRowSelectedIndex = e.RowIndex;
// Open your form here...
// And when your form returns...
// Set the selected index like so
dgv.Rows[_dataGridViewRowSelectedIndex].Selected = true;
}
这对你有帮助吗 您必须在函数
定时器_tick
private void timer3_Tick(object sender, EventArgs e)
{
int rowIndex;
if (dgvOrdini.Rows.Count == 0) //here I check if the dgv is empty
rowIndex = 0;
else
// I save the index of the current row in rowIndex
rowIndex = this.dgvOrdini.CurrentCell.RowIndex;
.......
.......
if (dgvOrdini.Rows.Count != 0) //Now if the dgv is not empty
//I set my rowIndex
dgvOrdini.CurrentCell = dgvOrdini.Rows[rowIndex].Cells[0];
}
使用此方法,选定的行不会更改。尝试此方法。
首先保存实际选中行的索引
int index = -1 //set the index to negative (because if you have only 1 row in your grid, this is a zero index
if (yourdatagridview.Rows.Count > 0) //if you have rows in datagrid
{
index = yourdatagridview.SelectedRows[0].Index; //then save index into variable
}
现在您可以更新datagridview
更新后,必须设置所选行:
if (index != -1) //if index == -1 then you don't have rows in your datagrid
{
yourdatagridview.Rows[index].Selected = true;
}
这很有效 在我单击一行后,一个新表单被打开。问题是,每次启用计时器时,都会调用populate_DatagridView方法,并且选定的行位于第一行,而不是保持选中的行单击。感谢您的回答,但在阅读了大量内容后,我认为我可以更好地解释我的问题。我的问题是,每次调用populate_datagridview方法时,除了实现数据之外,都会选择datagrid的第一行,而不是保持选中当前行或单击的行。基本上,我需要的是将FirstDisplayedScrollingRowIndex更新为当前行索引。这就是我的代码示例的基本功能,不是吗?有什么主要区别吗?你可能有我不知道的事。谢谢分享,如果是的话。=)