Click here to Skip to main content
13,189,324 members (69,087 online)
Rate this:
Please Sign up or sign in to vote.
See more:
private void Daily_Attendance_Load(object sender, EventArgs e)
      DataTable dailyattendance = new DataTable();
      dailyattendance = emp.viewallemployee(); //Function  for selecting all data from table
     for (int i = 0; i < dailyattendance.Rows.Count; i++)
         dataGridView1.Rows[i].Cells[0].Value = dailyattendance.Rows[i][0].ToString();                 
         dataGridView1.Rows[i].Cells[1].Value = dailyattendance.Rows[i][1].ToString();
         dataGridView1.Rows[i].Cells[2].Value = dailyattendance.Rows[i][2].ToString();
         dataGridView1.Rows[i].Cells[3].Value = dailyattendance.Rows[i][3].ToString();
Posted 3-Feb-13 22:00pm
jacobjohn196 4-Feb-13 4:02am
We need to display the data from the table using the datagridview. But it shows an error message "Index was out of range " when we try to assign the data from the data table to datagridview.
How can we solve this problem?
You hard-code indices 0 to 3, but how you guarantee that all that elements exist?
Use the debugger, and you will see.
jacobjohn196 4-Feb-13 4:09am
Thank you Sergey, But I have only four fields in the table.
Use the debugger or output/log exception stack, and you will see. Why taking guesses?
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

If you are using DataTable the simplest method is Binding the dataTable with your datagridview i.e
datagridView.DataSource = dailyAttendance;

You can find numerous article here in code project on how to bing dataTable to datagridview one is Important facts about datagridview[^]

Now coming back to the "Index was out of range" exception it may be when you try to access the row, the cells of the datagirview or the dataTable rows or cells which is not explained in your question. debug your application to identify which variable throws this exception datagrid or dataTable and add the columns to match.

my initial thought is you forget to add datagridviewRow instance to the Rows before you access 'dataGridView1.Rows[i]'
jacobjohn196 4-Feb-13 4:27am
Hai Jibesh.
Your initial thought was absolutely right.The datagridview.rows.add() line was missing in that code.
Now everything works fine.
Thanks once again.
jibesh 4-Feb-13 4:30am
Glad it helps.. I was going to add the code how to add the rows but you solved it before anyway am posting this.. if it helps someone in future.

int index = this.dataGridView1.Rows.Add();
this.dataGridView1.Rows[index].Cells[0].Value = dailyattendance.Rows[i][0].ToString();
this.dataGridView1.Rows[index].Cells[1].Value = dailyattendance.Rows[i][1].ToString();
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

Without knowing which exact line the error occurs on, we can't help you with a specific answer.
The best (indeed pretty much only) solution is to use the debugger.
Put a breakpoint on the first line in the method:
DataTable dailyattendance = new DataTable();
And single step though it.
You can examine each variable at each step and decide what should happen, then compare that with what did happen when you actually run the line.

Only then will you have anywhere near enough information to start solving this!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web04 | 2.8.171016.2 | Last Updated 4 Feb 2013
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100