Link to home
Start Free TrialLog in
Avatar of javagair
javagairFlag for United States of America

asked on

assigning dataset to gridview in vb.net

For i = 0 To Dstworkcounter2.Tables(0).Rows.Count - 1
       For j = 1 To 9
          DataGridView1.Item(j - 1, i).Value = Dstworkcounter2.Tables(0).Rows(i).Item(j).ToString
         Next
   Next
have a valid dataset, don't want the first value from it
get a run time error that says the index is either negative or out of range.
i is zero and j is one at that point.

I just want to fill the datagridview with data.  I remember something about turning off something in the datagridview properties.  
anyone got a idea why I just can't do a simple assign to a datagridview like this?

gary
Avatar of Fernando Soto
Fernando Soto
Flag of United States of America image

Hi javagair;

You should be able to do this and the second line of code will remove the column from being displayed.

DataGridView1.DataSource = Dstworkcounter2.Tables(0)
DataGridView1.Columns.Remove("Replace With Column Name As String")

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Ark
Ark
Flag of Russian Federation image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of javagair

ASKER

I added one simple line after reading ark comments.
 i = 0
                For Each row As DataRow In Dst3.Tables(0).Rows


                    DataGridView1.Rows.Add(1) 'THIS SOLVED PROBLEM
                    For j = 1 To 9
                        DataGridView1.Item(j - 1, i).Value = Dst3.Tables(0).Rows(i).Item(j).ToString
                    Next
                    i = i + 1
                Next row