【问题标题】:Selecting row that was dragged and dropped in datagridview选择在datagridview中拖放的行
【发布时间】:2018-07-08 08:23:04
【问题描述】:

我用一些示例数据创建了一个 datagridview。当我将 datagridview 中的一行拖到另一个位置时,效果很好。经过数小时的尝试后,我唯一无法工作的就是突出显示被拖动的行。 在我的代码中,该行在该行最初所在的位置突出显示。

下面是一些截图:

如您所见,我将第 8 行移至第 14 行,但行而不是突出显示第 14 行,它停留在第 8 行。

我的代码被剥离:

        Private Sub DataGridViewForm_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            LoadDataGridView1()
        End Sub

        Public Sub LoadDataGridView1()
            ' Fill datasource
        End Sub

        Private Sub Dgv_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles DatagridView1.DragDrop
           ' Code to move row up or down
        End Sub

        Private Sub Dgv_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles DatagridView1.DragEnter
            e.Effect = DragDropEffects.Copy
        End Sub

        Private Sub DataGridView_CellMouseDown(sender As Object, e As DataGridViewCellMouseEventArgs) Handles DatagridView1.CellMouseDown
            Dim dname As DataGridView = sender
            If e.Button = Windows.Forms.MouseButtons.Left Then
                    Dim view As DataGridViewRow = DirectCast(dname.Rows(e.RowIndex), DataGridViewRow)
                    If view IsNot Nothing Then
                        dname.DoDragDrop(view, DragDropEffects.Copy)
                    End If
                    LoadDataGridView1()
            End If
        End Sub

【问题讨论】:

  • 将拖放单元格的索引存储到一个变量中,然后使用该索引值设置突出显示。
  • 这是绑定数据源吗?
  • LoadDataGridView1() 使用如下命令从 SQL 表中获取数据: $"SELECT * FROM {GV.DB}{MainDB} ORDER BY LISTORDER" 所以是的,它是绑定数据资源。我添加了一个额外的列 LISTODER 来操作显示行的顺序,以便您可以拖放行。但即使我插入如下命令:
  • DataGridView1.Rows(10).Selected = True 在 LoadDataGridView1() 之后它不会突出显示第 10 行,它会继续突出显示第 8 行(拖动行的位置)
  • 今天我尝试了这个: LoadDataGridView1() MsgBox("After load ") DatagridView1.ClearSelection() MsgBox("After clear ") 并且在我拖放行后我收到消息“加载后" 并选择第一行,然后单击确定后没有选择任何内容,清除后出现消息,这样就可以了。在此消息上单击“确定”后,选择了最后单击的行。必须有一些内部指针来记住最后选择的行以及您所做的任何事情,即之后将选择的行。

标签: vb.net datagridview drag-and-drop selectedindex


【解决方案1】:

我为任何感兴趣的人修复了它: 在 CellMouseDown 我设置颜色:

  Private Sub Dgv_CellMouseDown(sender As Object, e As DataGridViewCellMouseEventArgs) Handles DgvPersonen.CellMouseDown
    Dim dname As DataGridView = sender
    dname.DefaultCellStyle.BackColor = Color.White
    dname.DefaultCellStyle.SelectionBackColor = Color.Tomato
    dname.AlternatingRowsDefaultCellStyle.BackColor = MyCustomColor
    dname.AlternatingRowsDefaultCellStyle.SelectionBackColor = Color.Tomato

当鼠标左键单击一行时,我选择该行并保存它的颜色:

        dname.CurrentCell = dname(e.ColumnIndex, e.RowIndex)
        OriginalColor = dname.Rows(OriginalRow).DefaultCellStyle.BackColor
        dname.Rows(OriginalRow).DefaultCellStyle.BackColor = OriginalColor
        dname.Rows(LastRow).DefaultCellStyle.BackColor = OriginalColor
        Dim view As DataGridViewRow = DirectCast(dname.Rows(e.RowIndex), DataGridViewRow)
        If view IsNot Nothing Then
            dname.DoDragDrop(view, DragDropEffects.Copy)
        End If

OriginalRow 和 LastRow 仅在拖放行时设置如下:

Private Sub Dgv_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles DgvPersonen.DragDrop
    Dim dname As DataGridView = sender
    dname.DefaultCellStyle.BackColor = Color.White
    dname.DefaultCellStyle.SelectionBackColor = Color.Tomato
    dname.AlternatingRowsDefaultCellStyle.BackColor = MyCustomColor
    dname.AlternatingRowsDefaultCellStyle.SelectionBackColor = Color.Tomato

    Dim newRow As DataGridViewRow = dname.Rows(dname.HitTest(p.X, p.Y).RowIndex)
    If newRow.Index >= 0 And newRow.Index < dname.Rows.Count Then
        ' Get the dropped row info
        Dim oldRow As DataGridViewRow = DirectCast(e.Data.GetData(GetType(DataGridViewRow)), DataGridViewRow)
        ' If the two rows aren't the same it inserts dropped row
        If newRow.Index <> oldRow.Index Then

            OriginalRow = oldRow.Index
            LastRow = newRow.Index
            OriginalColor = dname.Rows(OriginalRow).DefaultCellStyle.BackColor
            FillDataGridViewsOnValue()
            dname.Rows(OriginalRow).DefaultCellStyle.BackColor = OriginalColor
            dname.Rows(LastRow).DefaultCellStyle.BackColor = Color.Tomato
            dname.Rows(LastRow).Selected = True
            dname.DefaultCellStyle.BackColor = Color.White
            dname.DefaultCellStyle.SelectionBackColor = Color.White
            dname.AlternatingRowsDefaultCellStyle.BackColor = MyCustomColor
            dname.AlternatingRowsDefaultCellStyle.SelectionBackColor = MyCustomColor

经过大量试验和错误,但现在它的工作方式完全符合预期。

【讨论】:

  • 好吧,算了。上述解决方案只是操纵彩色行。不是选择什么。我完全取消了拖放功能,并使用 shift + 箭头键上下更改行。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-07-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-07-16
相关资源
最近更新 更多