VB.NET DataGridView 行位置调换

'Harry modify 2000


Public Class Form1

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        With DataGridView1
            .Columns.Add("No", "序号")
            .Columns.Add("GUID", "GUID")
            .Columns.Add("Header", "前8位")
            .Columns.Add("RowIndex", "行号")

            For i As Integer = 0 To 100
                Dim id As String = Guid.NewGuid().ToString
                Dim row As Integer = .Rows.Add()
                .Rows(row).Cells("No").Value = row + 1
                .Rows(row).Cells("GUID").Value = id
                .Rows(row).Cells("Header").Value = id.Substring(0, 8)
                .Rows(row).Cells("RowIndex").Value = row
            Next
            .AllowUserToAddRows = False  '对于下移、置底比较重要
        End With
    End Sub
    '仅支持连续选中的行 --- 上移
    Private Sub Up_Records(dgv As DataGridView)
        With dgv
            Try
                If .SelectedRows.Count > 0 Then
                    '获取选中行中最小行号
                    Dim minIndex As Integer = Integer.MaxValue
                    For i As Integer = 0 To .SelectedRows.Count - 1
                        If minIndex > .SelectedRows(i).Index Then
                            minIndex = .SelectedRows(i).Index
                        End If
                    Next
                    If minIndex > 0 Then  '确定不包含第一行
                        '选中行上一行移到所有选中行之后
                        Dim previousRow = .Rows(minIndex - 1)
                        .Rows.Remove(previousRow)
                        .Rows.Insert(minIndex + .SelectedRows.Count - 1, previousRow)
                    End If
                End If
            Catch ex As Exception
                MsgBox(ex.ToString)
            End Try
        End With
    End Sub
    '仅支持连续选中的行 --- 下移
    Private Sub Down_Records(dgv As DataGridView)
        With dgv
            Try
                If .SelectedRows.Count > 0 Then
                    '获取选中行中最大行号
                    Dim maxIndex As Integer = 0
                    For i As Integer = 0 To .SelectedRows.Count - 1
                        If maxIndex < .SelectedRows(i).Index Then
                            maxIndex = .SelectedRows(i).Index
                        End If
                    Next
                    If maxIndex < .Rows.Count - 1 Then  '确定不包含最后一行
                        '选中行后一行移到所有选中行之前
                        Dim nextRow = .Rows(maxIndex + 1)
                        .Rows.Remove(nextRow)
                        .Rows.Insert(maxIndex - .SelectedRows.Count + 1, nextRow)
                    End If
                End If
            Catch ex As Exception
                MsgBox(ex.ToString)
            End Try
        End With
    End Sub
    '支持非连续选中的行 --- 置顶
    Private Sub Top_Records(dgv As DataGridView)
        With dgv
            If .SelectedRows.Count > 0 Then
                '所有选择行 添加到排序字典(rowIndex,dataRow)
                Dim selectedRows_SortedDictionary As New SortedDictionary(Of Integer, DataGridViewRow)
                For i As Integer = 0 To .SelectedRows.Count - 1
                    selectedRows_SortedDictionary.Add(.SelectedRows(i).Index, .SelectedRows(i))
                Next
                '按字典顺序逐一删除原位置对应记录,并添加对应记录到新位置
                Dim num As Integer = 0
                For Each kvp In selectedRows_SortedDictionary
                    .Rows.Remove(kvp.Value)
                    .Rows.Insert(num, kvp.Value)
                    num += 1
                Next
                '清除原选择位置,根据新位置重新设置选择行
                .ClearSelection()
                For i = 0 To selectedRows_SortedDictionary.Count - 1
                    .Rows(i).Selected = True
                Next
            End If
        End With
    End Sub
    '支持非连续选中的行 --- 置底
    Private Sub Bottom_Records(dgv As DataGridView)
        With dgv
            If .SelectedRows.Count > 0 Then
                '所有选择行 添加到排序字典(rowIndex,dataRow)
                Dim selectedRows_SortedDictionary As New SortedDictionary(Of Integer, DataGridViewRow)
                For i As Integer = 0 To .SelectedRows.Count - 1
                    selectedRows_SortedDictionary.Add(.SelectedRows(i).Index, .SelectedRows(i))
                Next
                '按字典顺序逐一删除原位置对应记录,并添加对应记录到新位置
                For Each kvp In selectedRows_SortedDictionary
                    .Rows.Remove(kvp.Value)
                    .Rows.Insert(.Rows.Count, kvp.Value)
                Next
                '清除原选择位置,根据新位置重新设置选择行
                .ClearSelection()
                For i = 0 To selectedRows_SortedDictionary.Count - 1
                    .Rows(.Rows.Count - 1 - i).Selected = True
                Next
            End If
        End With
    End Sub

    Private Sub Btn_Up_Click(sender As Object, e As EventArgs) Handles Btn_Up.Click
        Up_Records(DataGridView1)
    End Sub

    Private Sub Btn_Next_Click(sender As Object, e As EventArgs) Handles Btn_Down.Click
        Down_Records(DataGridView1)
    End Sub

    Private Sub Btn_Top_Click(sender As Object, e As EventArgs) Handles Btn_Top.Click
        Top_Records(DataGridView1)
    End Sub

    Private Sub Btn_Bottom_Click(sender As Object, e As EventArgs) Handles Btn_Bottom.Click
        Bottom_Records(DataGridView1)
    End Sub
End Class

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值