Database Changes and new datagridview error


Well-known member
Dec 11, 2017
Programming Experience

After adding some data to a table by pasting from excel, my program can now no longer read the range of the datagridview.

The error is

'Index was out of range. Must be non-negative and less than the size of the collection.

Parameter name: index'

[SIZE=2]The code is here:

// Set Row 1 of dgvJobDetail

[/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=2][FONT=Consolas][SIZE=2]                [COLOR=#FF0000]dgvJobDetail.CurrentCell = dgvJobDetail.Rows[0].Cells[0];[/COLOR]

                dgvJobDetail.Rows[0].Selected = 
[/SIZE][/FONT][/SIZE][/FONT][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff]var[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=2][FONT=Consolas][SIZE=2] args2 = [/SIZE][/FONT][/SIZE][/FONT][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff]new[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=2][FONT=Consolas][SIZE=2] DataGridViewCellEventArgs(0, 0);[/SIZE][/FONT][/SIZE][/FONT]
                dgvJobDetail_CellClick(dgvJobDetail, args2);


I have tried many things including deleting the tables and re-adding to the project (edmx), as well as refreshing from the database. I even created a new project to attempt starting over, as well as deleted and re-added the datagridviews to the form.

Please let me know what I am not doing that needs to be done.
I think the real question here is where are the binding sources defined that are used by the datagridview? It seems the problem is that there is no data to put in it, all of a sudden. In order to add data, I had to effect the table by removing and re-adding primary keys.
Firstly, please provide a full and clear explanation of your problem. If an exception occurs then what line it occurs on is always relevant information.

If you se an ArgumentOutOfRangeException then the first thing to do is determine what the argument is and what the valid range for that argument is. Maybe you did that but there's no indication of it in your post. That's always relevant information.

That 'dgvJobDetail_CellClick' is an event handler and you should never be calling an event handler directly. If it contains some code that you want to execute other than when that event is raised then take that code out of the event handler and put it into it's own method. You can then call that method from the event handler and from anywhere else as well.

A BindingSource is a component and it will have been added to the form in the designer, just like the DataGridView. The base data source is a DataTable in your DataSet. That is bound to the BindingSource and the BindingSource is bound to the grid. If the grid isn't showing any data then it's not the fault of the BindingSource because it simply exposes what the DataTable provides. The DataTable will be being populated by a call to Fill on a table adapter. That is where you should be looking. That method call returns a number that represents the number of records retrieved from the database. If that number is zero then that means there was no data in the database that matched your query. It's possible that that Fill call is failing altogether and that's why your DataTable is empty.

You need to debug your code and watch that Fill call to see whether it succeeds and what it returns. If it does succeed and return zero then you need to look at your query and your database. If it returns other than zero and you see no data still then there's an issue with your data-binding and you can look at that for a fix. If it fails then you need to examine the exception to determine what went wrong.
Good morning, or perhaps evening in your case.

This is the only reference to a fill anything on the form designer.
[/SIZE][/FONT][FONT=Consolas][SIZE=2][COLOR=#008000][FONT=Consolas][SIZE=2][COLOR=#008000][FONT=Consolas][SIZE=2][COLOR=#008000]// jobDetailTableAdapter[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT]
[/SIZE][/FONT][/SIZE][/FONT][FONT=Consolas][SIZE=2][COLOR=#008000][FONT=Consolas][SIZE=2][COLOR=#008000][FONT=Consolas][SIZE=2][COLOR=#008000]// [/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT]
[/SIZE][/FONT][/SIZE][/FONT][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff]this[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=2][FONT=Consolas][SIZE=2].jobDetailTableAdapter.ClearBeforeFill = [/SIZE][/FONT][/SIZE][/FONT][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff]true[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=2][FONT=Consolas][SIZE=2];[/SIZE][/FONT][/SIZE][/FONT]
The out of range error happens here:
dgvJobDetail.CurrentCell = dgvJobDetail.Rows[0].Cells[0];

and I am convinced the records are not even being read.

Specifically, I opened the table (JobDetail), removed primary keys, and pasted in new data. Then I re-added the primary keys. I have also removed and re-added the tables in VS, and redefined the foreign keys. This is when the problem started, before anything was changed in the project. I even restored a project from last week, same issue.

Thanks for all of your time and explanation on this!
Last edited:
So there are two tables and two data grids.

table 1 is Jobs
table 2 is JobDetail

Table 1 fills the datagrid, and table 2 does not, though it did on Monday, before I modified the JobDetail table in SQL Server.

I am adding the datagridview and selecting the datasource at that time, basically the Binding Sources that do exist on the form, with the other controls

Jobs has a binding source called jobsBindingSource
JobDetail has a binding source called jobDetailBindingSource1

Perhaps I need to recreate the second Binding Source, is it corrupt? Have you seen this kind of thing before?

Thanks again!
Last edited:
The two Binding Datasources feed in to another one, ResourcePlanningDataSetBindingSource. This looks at the DataSet ResourcePlanningDataSet, which has a graphical image of all the tables, and that is where the Fill,GetData() is located. All the tables have this.

This may not be the best way to go, as this stuff is all automatically created. Also, why are there two Binding Sources feeding another none? Is this right?

Thanks again]!
If you have two related DataTables in a DataSet then there are two ways you can go with binding them:

1. Bind each DataTable to a BindingSource and bind each BindingSource to a DataGridView. In that case, you will see all records in both tables at all times.

2. Bind the parent table (Jobs for you) to a BindingSource and bind that to a DataGridView. The result is as for option 1 for the parent table. Next, bind the parent BindingSource to another BindingSource and set the DataMember to the DataRelation between the two DataTables. Bind that second BindingSource to another DataGridView. That second DataGridView will now display the records from the child table (JobDetail) but it will be filtered based on the parent record currently selected, i.e. you will only see those child records that are related to the current parent.
I believe my issue here is when I added records in SQL Server, and had to remove primary keys to manipulate it, that the DataSet is not excluding the field. The following fields are primary keys.


There is a one to many relationship define in C# for Jobs.ID to JobDetail.Jobs.ID

I went in and 'Configured' the JobDetail table in the xsd file, and modified the select statement to add the missing field. This also created a new QTY1 field, through there was already a QTY field and I do not see the relation.

Preferably, I would love to just delete the table and re-add, but I cannot figure out how this is done in this dataset, called ResourcePlanningDataset4. The relationship is already defined in Model1.emdx. Does it need to be in both?

I do not understand why I need to have a ResourcePlanningDataSetBindingSource in addition to a JobsBindingSource and JobDetailBindingSource1. The project just set all this stuff up automatically.

The ResourcePlanningDataSetBindingSource uses ResourcePlanningDataSet as its DataSource, as does the jobsBindingSource. However, the jobDetailBindingSource1 uses ResourcePlanningDataSetBindingSource for a DataSource.

I did restore this project to an older version when it worked, but now it does not. I have also re-created everything from scratch and am experiencing the same problem.

The dgv for the JobDetail is not populating. This is the problem.

Thanks for the help.

I have pretty much started the whole thing over. am confused about this instruction:
Next, bind the parent BindingSource to another BindingSource and set the DataMember to the DataRelation between the two DataTables.

Here is my entire process:
1. I create a foreign key relationship between the parent table (Jobs) and the Child Table (JobDetail).

Screen Shot 01-22-18 at 12.56 PM 001.PNG

2. You can see that the first datagridview (parent) has its datasource defined.


3. And you can see that the second datagridview (child) also has this. Under Data Member, where I am supposed to be able to reference the relationship created in step one, there are no options. Yes, everything was saved beforehand.

Screen Shot 01-22-18 at 12.57 PM 001.PNG

So I have obviously misunderstood something. The good news is, the datagridviews are reading the tables again.
Think you missed this step:
Next, bind the parent BindingSource to another BindingSource and set the DataMember to the DataRelation between the two DataTables.
Select the BindingSource in designer, there you set its DataSource/DataMember properties.

Along the same lines, I wish to populate a single datagrid with data from four tables. Attachment shows xsd.

Screen Shot 02-01-18 at 04.12 PM.PNG

Datagrid has a DataSource of timeBindingSource (that's above)
timeBindingSource is referenced on the form and has a DataSource of timeDataSet

Or... Do I need to have four Binding Sources setup, one for each table?

In this instance, TimeData is what I am primarily after adding to the DataGrid. I will have a textbox that limits it to the EmployeeID, and it will reference an existing Project Number (reference one field from Jobs), and an existing Activity (reference one from Activity).

In these cases, Employee has a parent relationship to TimeData, and TimeData has parent relationships to Jobs and Activity. All three relationships are setup as Foreign Keys as well.

Thanks in advance!
If you only want to display data from a single column of each of the other tables then you may be able to do it via a combo box column. You can bind the parent table to the column and display text from it instead of the foreign key value in the primary table. If you need multiple columns from a related table though, you'd have to add a new DataTable to your DataSet that was populated by a query that joined multiple tables.
Top Bottom