遍历DataGridView

    Public Function dvtodt(ByVal 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

你可能感兴趣的:(J#)