获取选定的非活动datagridview行

时间:2013-01-07 11:00:36

标签: vb.net

当我在当前处于活动状态的datagridview2的某个事件处理程序中时,我必须知道在另一个(非活动)datagridview1上选择了哪一行datagridview1(MultiSelect = False)。

我尝试了一些尝试,但没有正确的结果。

   Private Sub DataGridView2_CellDoubleClick(ByVal sender As Object, _
           ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) _
           Handles DataGridView2.CellDoubleClick

   Dim myindex1 As Integer = DataGridView1.CurrentCell.RowIndex

   OR

   Dim myindex1 As Integer = DataGridView1.CurrentRow.Index

当datagridview1处于活动状态并且我从其事件处理程序获取索引时,这些都不起作用。

我应该怎么做以及如何准确地从第二个datagridview的事件处理程序中获取第一行datagridview的选定行?

编辑: 添加了代码:

Private Sub datagridview1_RowLeave(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.RowLeave
    selrow1 = e.RowIndex
End Sub

Private Sub datagridview1_RowPrePaint(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewRowPrePaintEventArgs) Handles DataGridView1.RowPrePaint
    e.PaintParts = e.PaintParts And Not DataGridViewPaintParts.Focus
End Sub

Private Sub datagridview1_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles DataGridView1.GotFocus
    DataGridView1.RowsDefaultCellStyle.SelectionBackColor = Color.FromKnownColor(KnownColor.Highlight)
End Sub

Private Sub datagridview1_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles DataGridView1.LostFocus
    DataGridView1.RowsDefaultCellStyle.SelectionBackColor = Color.FromKnownColor(KnownColor.InactiveCaption)
End Sub

0 个答案:

没有答案