Solved

Index out of range error on datagridview?

Posted on 2014-09-10
6
182 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…

912 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

19 Experts available now in Live!

Get 1:1 Help Now