Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 303
  • Last Modified:

Simple table doesn't display (newbie)

A datagrid and a button exist on a form. When I click the button, the code below should create a new dataset, create a new table, create a single new column, then add 10 rows, then bind the datasource to the Datagrid for display.

My message box tells me that there are 10 rows, but the table doesn't display.

Why?
------------

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim DS As New DataSet()
        Dim DT As New DataTable()
        Dim DR As DataRow

        DT.TableName = "Yo"
        DS.Tables.Add("Yo")

        DT.Columns.Add("Raj")

        Dim iIndex As Long
        For iIndex = 1 To 10
            DR = DT.NewRow
            DR("Raj") = Str$(iIndex)
            DT.Rows.Add(DR)
        Next

        MessageBox.Show(DT.Rows.Count.ToString)

        DataGrid1.SetDataBinding(DS, "Yo")

    End Sub
0
rdavis101
Asked:
rdavis101
1 Solution
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
The problem was you never actually added your DataTable to your DataSet.  Instead, you created a new table called "Yo" and then added all the records to your local DataTable:    

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim DS As New DataSet
        Dim DT As New DataTable
        Dim DR As DataRow

        DT.TableName = "Yo"
        ' DS.Tables.Add("Yo") ' <---------- This created a new, different table in DS called "Yo"
        DS.Tables.Add(DT) ' <---------- Add the table you are working with to the DataSet

        DT.Columns.Add("Raj")

        Dim iIndex As Long
        For iIndex = 1 To 10
            DR = DT.NewRow
            DR("Raj") = Str$(iIndex)
            DT.Rows.Add(DR)
        Next

        MessageBox.Show(DT.Rows.Count.ToString)

        DataGrid1.SetDataBinding(DS, "Yo")

    End Sub

Regards,

{pseudocode} / {Time} ± ¼*ƒ(Me.Thoughts÷3)^² = Idle »(°_°)« Mind
0
 
rdavis101Author Commented:
Thanks, dude.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now