2015-07-10 53 views
0

我想獲得一行的索引。該行將從dataGridView拖到一個treeView上。我正在試圖在dataGridView的MouseDown事件上獲取索引,然後纔開始拖動它。如何獲取從dataGridView拖動到不同控件的行的索引?

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)

回答

0

您應該使用不同的事件,請嘗試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); 
    } 
} 
+0

ha!完善!我用mouseDown事件嘗試了很多不同的方式,我開始瘋了!謝謝! – adarom1987

相關問題