Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

assigning dataset to gridview in vb.net

Posted on 2013-12-27
3
Medium Priority
?
330 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 64

Expert Comment

by:Fernando Soto
ID: 39743148
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 28

Accepted Solution

by:
Ark earned 2000 total points
ID: 39744267
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
ID: 39748443
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

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Suggested Courses

885 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