Solved

Index out of range error on datagridview?

Posted on 2014-09-10
6
180 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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
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…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

760 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

18 Experts available now in Live!

Get 1:1 Help Now