K
Kareninstructor
Guest
Over the years I've seen countless questions on topics like "how do I export a DataGridView contents to Excel?" and similar questions like that. I've never used a DataGridView without a DataSource so the majority of my replies to these questions is to setup a DataSource yet many will not and that is okay. So to work with these developers and not have to iterate rows/cells to produce a DataTable in their form the following is one idea to convert a DataGridView data into a DataTable without concern for matching data types of cell values, I kept it very simple. I have no plans on using this other than answering questions.
Option Strict On
Option Infer Off
Public Module DataGridViewExtensionMethods
''' <summary>
''' Given a DataGridView populates without a data source,
''' create a DataTable, populate from rows/cells from the
''' DataGridView with an option to include/exclude column names.
''' </summary>
''' <param name="pDataGridView"></param>
''' <param name="pColumnNames"></param>
''' <returns></returns>
''' <remarks>
''' There is no attempt made to figure out data types coming
''' from data in the DataGridView
''' </remarks>
<Runtime.CompilerServices.Extension>
Public Function GetDataTable(ByVal pDataGridView As DataGridView, Optional ByVal pColumnNames As Boolean = True) As DataTable
Dim dt As DataTable = New DataTable()
For Each column As DataGridViewColumn In pDataGridView.Columns
If column.Visible Then
If pColumnNames Then
dt.Columns.Add(New DataColumn() With {.ColumnName = column.Name})
Else
dt.Columns.Add()
End If
End If
Next
Dim cellValues(pDataGridView.Columns.Count - 1) As Object
For Each row As DataGridViewRow In pDataGridView.Rows
For i As Integer = 0 To row.Cells.Count - 1
cellValues(i) = row.Cells(i).Value
Next
dt.Rows.Add(cellValues)
Next
Return dt
End Function
End Module
Usage
Dim dt As DataTable = DataGridView1.GetDataTable
So far I've used the extension method (in C#) this morning here. Then converted it to VB.NET thinking it might be of some use.
In closing, I don't advocate populating a DataGridView w/o a DataSource yet it does happen and this extension may assist those who don't want to change yet need a simple DataTable from a DataGridView.
Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
VB Forums - moderator
Continue reading...
Option Strict On
Option Infer Off
Public Module DataGridViewExtensionMethods
''' <summary>
''' Given a DataGridView populates without a data source,
''' create a DataTable, populate from rows/cells from the
''' DataGridView with an option to include/exclude column names.
''' </summary>
''' <param name="pDataGridView"></param>
''' <param name="pColumnNames"></param>
''' <returns></returns>
''' <remarks>
''' There is no attempt made to figure out data types coming
''' from data in the DataGridView
''' </remarks>
<Runtime.CompilerServices.Extension>
Public Function GetDataTable(ByVal pDataGridView As DataGridView, Optional ByVal pColumnNames As Boolean = True) As DataTable
Dim dt As DataTable = New DataTable()
For Each column As DataGridViewColumn In pDataGridView.Columns
If column.Visible Then
If pColumnNames Then
dt.Columns.Add(New DataColumn() With {.ColumnName = column.Name})
Else
dt.Columns.Add()
End If
End If
Next
Dim cellValues(pDataGridView.Columns.Count - 1) As Object
For Each row As DataGridViewRow In pDataGridView.Rows
For i As Integer = 0 To row.Cells.Count - 1
cellValues(i) = row.Cells(i).Value
Next
dt.Rows.Add(cellValues)
Next
Return dt
End Function
End Module
Usage
Dim dt As DataTable = DataGridView1.GetDataTable
So far I've used the extension method (in C#) this morning here. Then converted it to VB.NET thinking it might be of some use.
In closing, I don't advocate populating a DataGridView w/o a DataSource yet it does happen and this extension may assist those who don't want to change yet need a simple DataTable from a DataGridView.
Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
VB Forums - moderator
Continue reading...