Solved

Simple table doesn't display (newbie)

Posted on 2004-09-03
2
296 Views
Last Modified: 2010-04-23
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
Comment
Question by:rdavis101
2 Comments
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 50 total points
ID: 11977695
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
 

Author Comment

by:rdavis101
ID: 11977753
Thanks, dude.
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now