thongkorn โพสต์ 2019-5-7 18:08:56

[VB.NET] การเคลื่อนย้ายตำแหน่งแถวที่เลือกในตารางกริด ให้ขึ้นบนหรือลงล่าง

http://www.g2gnet.com/webboard/images/vbnet/moverowdgv.png


หลักการคิดสำหรับการเคลื่อนย้ายแถวขึ้นบน : เลือกแถวที่ต้องการ ตรวจสอบว่าแถวนั้นต้องมีค่า Index มากกว่า 0 (แถวแรก Index = 0) จากนั้นคัดลอกรายการแถวนั้นๆเอาไว้ก่อน เพื่อเก็บค่าไว้ในตัวแปรแบบแถวของตารางกริด (Dim Row As DataGridViewRow) แล้วทำการลบแถวที่เลือกออกไป สุดท้ายให้แทรก (Insert) แถวที่คัดลอกเอาไว้ นำไปวางไว้หน้าแถวที่เลือกลบออกด้วย 1 ก็จะทำให้เสมือนเกิดการเคลื่อนย้ายตำแหน่งแถวได้นั่นเอง ... สำหรับการเคลื่อนย้ายแถวลง ก็คิดเหมือนกัน แต่คำนวณหาจำนวนแถวสูงสุด


มาดูโค้ดกันเถอะ ...
Public Class frmDataGridMoveRow

    Private Sub frmDataGridMoveRow_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
      Call InitDataGrid()
    End Sub

    ' / --------------------------------------------------------------------------------
    ' / Initialized DataGridView and put the sample data.
    Private Sub InitDataGrid()
      '// Initialize DataGridView Control
      With DataGridView1
            .RowHeadersVisible = False
            .AllowUserToAddRows = False
            .AllowUserToDeleteRows = False
            .AllowUserToResizeRows = False
            .MultiSelect = False
            .SelectionMode = DataGridViewSelectionMode.FullRowSelect
            .ReadOnly = True
            .Font = New Font("Tahoma", 9)
            ' Autosize Column
            .AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill
            .AutoResizeColumns()
            '// Even-Odd Color
            .AlternatingRowsDefaultCellStyle.BackColor = Color.AliceBlue
            ' Adjust Header Styles
            With .ColumnHeadersDefaultCellStyle
                .BackColor = Color.Navy
                .ForeColor = Color.Black ' Color.White
                .Font = New Font("Tahoma", 9, FontStyle.Bold)
            End With
      End With
      '// Declare columns type.
      Dim Column1 As New DataGridViewTextBoxColumn()
      Dim Column2 As New DataGridViewTextBoxColumn()
      Dim Column3 As New DataGridViewTextBoxColumn()
      '// Add new Columns
      DataGridView1.Columns.AddRange(New DataGridViewColumn() { _
                Column1, Column2, Column3 _
                })
      With DataGridView1
            .Columns(0).Name = "Product ID"
            .Columns(1).Name = "Product Name"
            .Columns(2).Name = "Unit Price"
      End With

      '// SAMPLE DATA
      Dim RandomClass As New Random()
      For iCount As Byte = 1 To 10
            Dim row = New String() { _
            iCount, "Product " & iCount, Format(RandomClass.Next(999) + RandomClass.NextDouble(), "0.00")}
            DataGridView1.Rows.Add(row)
      Next
    End Sub

    ' / --------------------------------------------------------------------------------
    ' / Move Up.
    Private Sub btnUp_Click(sender As System.Object, e As System.EventArgs) Handles btnUp.Click
      With Me.DataGridView1
            '// หาค่า Index แถวที่เลือก
            Dim RowIndex As Integer = .SelectedCells(0).OwningRow.Index
            '// หาก Index = 0 แสดงว่าเป็นแถวบนสุด ให้จบออกจากโปรแกรมย่อย
            If RowIndex = 0 Then Return
            '//
            Dim Col As Integer = .SelectedCells(0).OwningColumn.Index
            Dim Rows As DataGridViewRowCollection = .Rows
            '// เก็บค่าแต่ละเซลล์ของแถวที่เลือก
            Dim Row As DataGridViewRow = Rows(RowIndex)
            '// ลบแถวที่เลือกออก
            Rows.Remove(Row)
            '// ไปเพิ่มแถวใหม่ ก่อนแถวที่เลือก 1 แถว (ก็เลยเสมือนมันเคลื่อนย้ายแถวได้)
            Rows.Insert(RowIndex - 1, Row)
            '// เคลียร์การเลือกแถว
            .ClearSelection()
            '// โฟกัสรายการแถวที่เลื่อนขึ้นไปแทรก
            .Rows(RowIndex - 1).Cells(Col).Selected = True
      End With
    End Sub

    ' / --------------------------------------------------------------------------------
    ' / Move Down.
    Private Sub btnDown_Click(sender As System.Object, e As System.EventArgs) Handles btnDown.Click
      With Me.DataGridView1
            Dim RowIndex As Integer = .SelectedCells(0).OwningRow.Index
            If RowIndex = .Rows.Count - 1 Then Return
            '//
            Dim Col As Integer = .SelectedCells(0).OwningColumn.Index
            Dim Rows As DataGridViewRowCollection = .Rows
            Dim Row As DataGridViewRow = Rows(RowIndex)
            Rows.Remove(Row)
            Rows.Insert(RowIndex + 1, Row)
            .ClearSelection()
            '//
            .Rows(RowIndex + 1).Cells(Col).Selected = True
      End With
    End Sub
End Class

ดาวน์โหลดโค้ดต้นฉบับ VB.NET (2010) ได้ที่นี่ ...

หน้า: [1]
ดูในรูปแบบกติ: [VB.NET] การเคลื่อนย้ายตำแหน่งแถวที่เลือกในตารางกริด ให้ขึ้นบนหรือลงล่าง