我在 WinForms datagridview 中有以下代码用于处理鼠标右键单击以选择基础行:
private void dataGridViewTestSteps_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button != MouseButtons.Right) return;
var hitTestInfo = dataGridViewTestSteps.HitTest(e.X, e.Y);
dataGridViewTestSteps.ClearSelection();
dataGridViewTestSteps.Rows[hitTestInfo.RowIndex].Selected = true;
}
...现在这工作正常,但它没有将小指示器放在正确的行中(见下图)。所以基本上我想知道上面的方法中缺少什么?
最佳答案
行标题光标显示当前行,而不是选定的行 - 它们实际上是不同的,因为您可以有多个选定的行但只有一个当前行。
试试这个代码:
private void dataGridViewTestSteps_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button != MouseButtons.Right) return;
var hitTestInfo = dataGridViewTestSteps.HitTest(e.X, e.Y);
//dataGridViewTestSteps.ClearSelection();
//dataGridViewTestSteps.Rows[hitTestInfo.RowIndex].Selected = true;
dataGridViewTestSteps.CurrentCell = dataGridViewTestSteps.Rows[hitTestInfo.RowIndex].Cells[0];
}
关于.net - 为手动选择的行放置 DataGridView 的行指示器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6818220/