I was looking for code for making a data table which I could fill with what ever information I wanted to fill it with, and then use it for what ever I want to use it for. Its mainly because some of the things I’m working on are in multiple tables and even using an inner joins don’t help. So, this way I can create a datatable with the information I want in it, return it via either a webservice or what ever, and it works grand. The following is the code im using.

 

        Dim dt As New DataTable ‘ create an empty table

        Dim row As DataRow ‘ new row

        Dim list As New ArrayList ‘ array list that holds the row information

        Dim i As Integer

        For i = 0 To 6 ‘ create 7 colums in the row

            list.Add(New DataColumn(i)) ‘create the new colum

            dt.Columns.Add(list(i)) ‘ name the row as the value of i

        Next

        For i = 0 To 200 ‘ now create 201 rows and add them to the table

            Dim myRow As DataRow

            ‘ Create a new DataRow.

            myRow = dt.NewRow()

            Dim z As Integer

            For z = 0 To 6 ‘ put data in each colum

                myRow(z) = i

            Next

            dt.Rows.Add(myRow)

            dt.AcceptChanges() ‘ accept the changes in the table

        Next

        dt.AcceptChanges() ‘ accept the changes again, just in case

        DataGrid1.DataSource = dt ‘ set the datasource of the datagrid to the table

After reading the code, I think there could be a quicker way of doing the following code:

        For i = 0 To 6 ‘ create 7 colums in the row

            list.Add(New DataColumn(i)) ‘create the new colum

            dt.Columns.Add(list(i)) ‘ name the row as the value of i

&
nbsp;  &nb
sp;    Next

Replace the code with:

        For i = 0 To 6 ‘ create 7 colums in the row

            dt.Columns.Add(New DataColumn(i)) ‘ name the row as the value of i

        Next

Not sure if it speeds up anything, but in theory it should. You could also remove the list variable. Hope this helps someone!