Solved

Index out of range error on datagridview?

Posted on 2014-09-10
6
199 Views
Last Modified: 2014-09-10
I'm getting an "Index out of range error on a datagridview and not sure why. I'm assigning a datasource to it that contains 4 rows with 8 columns. I've attached a screenshot of my debugging session. I execute "BL.GetLoadData" to load the DataSet. EH is an instance of an Error Handling class.
Screenshot.jpg
0
Comment
Question by:BlakeMcKenna
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
6 Comments
 
LVL 40
ID: 40315001
Have you verified that your table has columns before setting it as the DataSource.

Somebody had a similar problem a few days ago, and they found out that it was setting the grid to Nothing before changing its DataSource suddenly started to cause problems.
0
 

Author Comment

by:BlakeMcKenna
ID: 40315366
Yes, it has 8 columns and 4 rows of data...
0
 

Author Comment

by:BlakeMcKenna
ID: 40315745
I wanted to say that the Columns for this dgv are built in the designer and not thru the SQL Query.
0
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 

Author Comment

by:BlakeMcKenna
ID: 40315806
James,

I just checked mine and it's nothing as well but what do I need to do if it's being built as a result of a SQL Query? As I mentioned above, I build the Grid Columns in designer mode. The DataNameProperty is empty!
0
 
LVL 40

Accepted Solution

by:
Jacques Bourgeois (James Burger) earned 500 total points
ID: 40315898
Ooops. An interesting new piece of information there. If the columns are built in the designer, then you probably lose them when you set the DataSource to Nothing.

Instead of doing so, simply call Clear on the grid. This removes the old data but should keep the formatting.

Nowadays, I use something else than the DataGridView. But when I did, I never liked the designer and preferred setting the format through my code. Setting the DataSource then automatically creates the columns, all that is left is to format them. It took more time, but since I had complete control and often needed to add things that the designer could not do anyway, I found it to be worth the trouble.

Here is an example, where dgvComponent is the grid and _componentList is the source of data:
		With dgvComponents

			.IsInitializing = True
			.DataSource = Nothing
			.DataSource = _componentsList

			.RowHeadersWidthSizeMode = DataGridViewRowHeadersWidthSizeMode.DisableResizing
			.RowHeadersWidth = 15

			'Columns that need to be hidden
			.Columns("ProjectID").Visible = False
			.Columns("ID").Visible = False
			.Columns("HasChanged").Visible = False
			.Columns("NumberOfParts").Visible = False

			'Setup of the remaining columns
			With .Columns("Quantity")
				.HeaderText = "Qt"
				.ToolTipText = My.Resources.TltQuantity
				.DisplayIndex = 0
				.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
				.AutoSizeMode = DataGridViewAutoSizeColumnMode.None
				.Width = 30
			End With

			'And so on....

Open in new window

0
 

Author Closing Comment

by:BlakeMcKenna
ID: 40315920
Thanks James...that worked perfect! There wasn't much I had to do but that "Clear" Method worked great!
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Come and listen to Percona CEO Peter Zaitsev discuss what’s new in Percona open source software, including Percona Server for MySQL (https://www.percona.com/software/mysql-database/percona-server) and MongoDB (https://www.percona.com/software/mongo-…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…

707 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