2011年5月9日 星期一

datagridview convert datatable

Public Function dvtodt(dv As DataGridView) As DataTable
Dim dt As New DataTable()
Dim dc As DataColumn
For i As Integer = 0 To dv.Columns.Count - 1
dc = New DataColumn()
dc.ColumnName = dv.Columns(i).HeaderText.ToString()
dt.Columns.Add(dc)
Next
For j As Integer = 0 To dv.Rows.Count - 2
Dim dr As DataRow = dt.NewRow()
For x As Integer = 0 To dv.Columns.Count - 1
dr(x) = dv.Rows(j).Cells(x).Value
Next
dt.Rows.Add(dr)
Next
Return dt
End Function

沒有留言:

張貼留言