簡體   English   中英

如何獲取從dataGridView拖動到其他控件的行的索引?

[英]How to acquire the Index of a Row that is being dragged from a dataGridView to a different control?

我正在嘗試獲取行的索引。 該行將從dataGridView拖到treeView上。 在嘗試開始拖動之前,我試圖獲取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);
        }
    }

讓我知道是否有更好的方法,因為就目前情況而言,這不會返回正確的索引,而是返回第一行(dataGridView的第一行)的索引

您應該使用其他事件,請嘗試使用CellMouseDown 在該事件的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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM