|
สำหรับบทความนี้ หลักการการทำงานก็ไม่ได้มีความยากเย็นหรือซับซ้อนอะไรเลย ใช้หลักการคัดลอกข้อมูลแถวลำดับ (Array) จากตารางกริดต้นฉบับ จากนั้นก็นำแถวข้อมูลที่ได้ไปเพิ่ม (Add) เข้าไปในตารางกริดอีกตัวก็จบล่ะ ...
มาดูโค้ดกันเถอะ ...
- ' / --------------------------------------------------------------------
- ' / Developer : Mr.Surapon Yodsanga (Thongkorn Tubtimkrob)
- ' / eMail : thongkorn@hotmail.com
- ' / URL: http://www.g2gnet.com (Khon Kaen - Thailand)
- ' / Facebook: https://www.facebook.com/g2gnet (For Thailand)
- ' / Facebook: https://www.facebook.com/commonindy (Worldwide)
- ' / Purpose: Copy rows from One DataGridView To Another One.
- ' / Microsoft Visual Basic .NET (2010)
- ' /
- ' / This is open source code under @CopyLeft by Thongkorn Tubtimkrob.
- ' / You can modify and/or distribute without to inform the developer.
- ' / --------------------------------------------------------------------
- Public Class frmCopyDataGrid
- '// Initialize DataGridView Control.
- Private Sub InitGrid(DGV As DataGridView)
- With DGV
- .AllowUserToAddRows = False
- .AllowUserToDeleteRows = False
- .AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill
- .AutoResizeColumns()
- End With
- '// Declare columns type.
- Dim Column1 As New DataGridViewTextBoxColumn()
- Dim Column2 As New DataGridViewTextBoxColumn()
- Dim Column3 As New DataGridViewTextBoxColumn()
- '// Add new Columns
- DGV.Columns.AddRange(New DataGridViewColumn() { _
- Column1, Column2, Column3 _
- })
- With DGV
- .Columns(0).Name = "ID"
- .Columns(1).Name = "Name"
- .Columns(2).Name = "Date"
- End With
- End Sub
- Private Sub frmCopyDataGrid_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
- '// Initialize DataGridView
- Call InitGrid(DataGridView1)
- Call InitGrid(DataGridView2)
- '// SAMPLE DATA
- Dim RandomClass As New Random()
- '// DateTime.Today.AddDays(-RandomClass.Next(365)) --> Random past date 365 days.
- Dim row As String() = New String() {"1", "Product 1", DateTime.Today.AddDays(-RandomClass.Next(365))}
- DataGridView1.Rows.Add(row)
- row = New String() {"2", "Product 2", DateTime.Today.AddDays(-RandomClass.Next(365))}
- DataGridView1.Rows.Add(row)
- row = New String() {"3", "Product 3", DateTime.Today.AddDays(-RandomClass.Next(365))}
- DataGridView1.Rows.Add(row)
- row = New String() {"4", "Product 4", DateTime.Today.AddDays(-RandomClass.Next(365))}
- DataGridView1.Rows.Add(row)
- End Sub
- Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
- '// Clear rows
- DataGridView2.Rows.Clear()
- '// Counts the number of rows in the grid.
- For iRow = 0 To DataGridView1.Rows.Count - 1
- '// Copy the original data in each row to the array variable.
- Dim row As String() = New String() {DataGridView1.Rows(iRow).Cells(0).Value, DataGridView1.Rows(iRow).Cells(1).Value, DataGridView1.Rows(iRow).Cells(2).Value}
- '// Add row in DataGridView2
- DataGridView2.Rows.Add(row)
- Next
- End Sub
- End Class
คัดลอกไปที่คลิปบอร์ด
ดาวน์โหลดโค้ดต้นฉบับ VB.NET (2010) ได้ที่นี่
|
ขออภัย! โพสต์นี้มีไฟล์แนบหรือรูปภาพที่ไม่ได้รับอนุญาตให้คุณเข้าถึง
คุณจำเป็นต้อง ลงชื่อเข้าใช้ เพื่อดาวน์โหลดหรือดูไฟล์แนบนี้ คุณยังไม่มีบัญชีใช่ไหม? ลงทะเบียน
x
|