Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#4.0
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 23:00pm
Comments
jacobjohn196 at 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?
Thanks
Sergey Alexandrovich Kryukov at 4-Feb-13 4:03am
   
You hard-code indices 0 to 3, but how you guarantee that all that elements exist?
Use the debugger, and you will see.
—SA
jacobjohn196 at 4-Feb-13 4:09am
   
Thank you Sergey, But I have only four fields in the table.
Sergey Alexandrovich Kryukov at 4-Feb-13 4:12am
   
Use the debugger or output/log exception stack, and you will see. Why taking guesses?
—SA
Rate this: bad
good
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]'
  Permalink  
Comments
jacobjohn196 at 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 at 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
good
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!
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 390
1 Sergey Alexandrovich Kryukov 329
2 BillWoodruff 210
3 Afzaal Ahmad Zeeshan 204
4 CPallini 185
0 OriginalGriff 5,515
1 DamithSL 4,451
2 Maciej Los 3,902
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,175


Advertise | Privacy | Mobile
Web04 | 2.8.141216.1 | Last Updated 4 Feb 2013
Copyright © CodeProject, 1999-2014
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