[英]How to acquire the Index of a Row that is being dragged from a dataGridView to a different control?
I am trying to get the index of a row. 我正在尝试获取行的索引。 The row is to be dragged from the dataGridView onto a treeView. 该行将从dataGridView拖到treeView上。 I was trying to get the index on the MouseDown event of the dataGridView, before anything has started to be dragged. 在尝试开始拖动之前,我试图获取dataGridView的MouseDown事件的索引。
private void dataGridView1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
var rowNum = dataGridView1.SelectedCells[0].RowIndex;
dragItemID = dataGridView1["ID", rowNum].Value.ToString();
dataGridView1.DoDragDrop(dragItemID, DragDropEffects.Copy);
}
}
Let me know if there is a better way to do it, because as it stands this doesn't return the correct index, and instead returns the index of the top row (the first row in the dataGridView) 让我知道是否有更好的方法,因为就目前情况而言,这不会返回正确的索引,而是返回第一行(dataGridView的第一行)的索引
You should use different event, try with CellMouseDown
. 您应该使用其他事件,请尝试使用CellMouseDown
。 Inside EventArgs
of that event you have information about RowIndex
. 在该事件的EventArgs
内部,您具有有关RowIndex
信息。
private void dataGridView1_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
dragItemID = dataGridView1["ID", e.RowIndex].Value.ToString();
dataGridView1.DoDragDrop(dragItemID, DragDropEffects.Copy);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.