Solved

Index out of range error on datagridview?

Posted on 2014-09-10
6
186 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
  • 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
How our DevOps Team Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

 

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

ScreenConnect 6.0 Free Trial

Explore all the enhancements in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

Question has a verified solution.

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

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 …
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.
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.

825 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