简体   繁体   English

如何获取从dataGridView拖动到其他控件的行的索引?

[英]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.

相关问题 如何从用户控件将Row添加到dataGridView - How to add Row to dataGridView from user control 如何防止禁用的表单被拖动? - How to prevent a disabled Form from being dragged? 如何从 DataGridView 控件的底部删除空行? - How do I remove the empty row from the bottom of a DataGridView control? WinRT Gridview DragItemsStarting 事件:如何找到被拖动的控件? - WinRT Gridview DragItemsStarting event: How to find control being dragged? 如何在DatagridView中隐藏从索引2的行开始直到结尾的行? - How to hide rows in DatagridView starting from row with index 2 and till the end? 如何不使用索引从datagridview中删除多行? - How to remove multiple row from datagridview without using index? 如何从 DataGridView 中的选定行中获取最后选定行的索引 - how to get index of the last selected row from a selection of rows in a DataGridView 如何在datagridView中获取行索引和列索引 - How to get the Row index and Columns index in the datagridView 如果选择了多个 datagridview 行,如何防止选择“新”datagridview 行中的单元格? - How can I prevent cells in the “new” datagridview row from being selected if there are multiple datagridview rows selected? 如何从不同的线程添加行并更改DataGridView中的单元格值? - How to add a row and change cell value in DataGridView from a different thread?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM