Solved

assigning dataset to gridview in vb.net

Posted on 2013-12-27
3
309 Views
Last Modified: 2013-12-31
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
0
Comment
Question by:javagair
3 Comments
 
LVL 62

Expert Comment

by:Fernando Soto
Comment Utility
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

0
 
LVL 27

Accepted Solution

by:
Ark earned 500 total points
Comment Utility
or         DataGridView1.Columns(0).Visible = False ( after setting datasource assuming AutoGenerateColumns=True)

You got this error because your datagridview initialy have no row/columns, so assigning value to first (zero-based==0) row and first(==0) column throw out of range error
0
 

Author Comment

by:javagair
Comment Utility
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
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
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 …
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

762 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

11 Experts available now in Live!

Get 1:1 Help Now