I found this code and have implemented it in my project but I don't fully understand a few things.
Does the line "Return dt" actually create the table?
There is also this code that outputs the table to .xml
That code does in fact output a .xml file that I can pull into excel to verify it contains data. However I would like to know how to bind "mydatatable" to a datavisualization chart on another form.
I don't know how to access the in memory data table to do that.
My modified code [similar to above] is this.
I remmed out the writexml line and fiddled with the code a bit in the next line in hope that it would output a table that I could find and bind to....but I can't find it an don't know how to access it. Understand I should be able to bind the chart to "mydatatable" via code but don't know the syntax.... HELP
VB.NET:
Public Shared Function DataGridViewToDataTable(ByVal dtg As DataGridView,
Optional ByVal DataTableName As String = "myDataTable") As DataTable
Try
Dim dt As New DataTable(DataTableName)
Dim row As DataRow
Dim TotalDatagridviewColumns As Integer = dtg.ColumnCount - 1
'Add Datacolumn
For Each c As DataGridViewColumn In dtg.Columns
Dim idColumn As DataColumn = New DataColumn()
idColumn.ColumnName = c.Name
dt.Columns.Add(idColumn)
Next
'Now Iterate thru Datagrid and create the data row
For Each dr As DataGridViewRow In dtg.Rows
'Iterate thru datagrid
row = dt.NewRow 'Create new row
'Iterate thru Column 1 up to the total number of columns
For cn As Integer = 0 To TotalDatagridviewColumns
row.Item(cn) = IfNullObj(dr.Cells(cn).Value) ' This Will handle error datagridviewcell on NULL Values
Next
'Now add the row to Datarow Collection
dt.Rows.Add(row)
Next
'Now return the data table
[B] Return dt[/B]
Catch ex As Exception
Return Nothing
End Try
End Function
Does the line "Return dt" actually create the table?
There is also this code that outputs the table to .xml
VB.NET:
Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
Dim xmlPath As String = "c:\DatagridviewXML.xml"
Call DataGridViewToDataTable(Me.DGV1, "mydatatable").WriteXml(xmlPath)
End Sub
End Class
That code does in fact output a .xml file that I can pull into excel to verify it contains data. However I would like to know how to bind "mydatatable" to a datavisualization chart on another form.
I don't know how to access the in memory data table to do that.
My modified code [similar to above] is this.
VB.NET:
Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
Dim xmlPath As String = "c:\DatagridviewXML.xml"
'Call DataGridViewToDataTable(Me.DGV1, "mydatatable").WriteXml(xmlPath)
Call DataGridViewToDataTable(Me.DGV1, "mydatatable").AsDataView()
charts.ShowDialog()
End Sub
End Class
I remmed out the writexml line and fiddled with the code a bit in the next line in hope that it would output a table that I could find and bind to....but I can't find it an don't know how to access it. Understand I should be able to bind the chart to "mydatatable" via code but don't know the syntax.... HELP