data table code explanation please

bones

Well-known member
Joined
Aug 23, 2014
Messages
143
Programming Experience
Beginner
I found this code and have implemented it in my project but I don't fully understand a few things.

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
 
Does the line "Return dt" actually create the table?
No, a Return statement always tells a function what value to pass back to the code that called it. This is the line that creates the DataTable:
VB.NET:
Dim dt As New DataTable(DataTableName)
Whenever you see the New keyword, an object is being created. There are some cases where an object will be created without a New keyword, e.g.
VB.NET:
row = dt.NewRow 'Create new row
but that NewRow method will use the New keyword internally to create the DataRow.
 
Thank you for those answers. Can you explain to me the following?

The first bit of code creates "mydatatable" from form2.dgv1. How do I access that table?

I want to use the data in "mydatatable" for a chart I have placed on form3.

eg. How would I tell the chart to use "mydatatable" as it's data source?
 
the table is returned from function:
Dim table = DataGridViewToDataTable(...)

pass it to the other form, possibly directly to the Chart control:
form3.TheChart.DataSource = table
 
Back
Top