Solved

Index out of range error on datagridview?

Posted on 2014-09-10
6
191 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
Industry Leaders: 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!

 

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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

Title # Comments Views Activity
VB.net and sql server 4 55
Convert Ctime to date time in textfile? 7 64
Need syntax help building hyperlink 2 42
Visual Studio hangs on running project 6 64
I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
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…

696 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