Solved

assigning dataset to gridview in vb.net

Posted on 2013-12-27
3
319 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 63

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 500 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
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 …
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

856 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