Click here to Skip to main content
15,893,594 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
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
Comments
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?
Thanks
Sergey Alexandrovich Kryukov 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 4-Feb-13 4:09am    
Thank you Sergey, But I have only four fields in the table.
Sergey Alexandrovich Kryukov 4-Feb-13 4:12am    
Use the debugger or output/log exception stack, and you will see. Why taking guesses?
—SA

If you are using DataTable the simplest method is Binding the dataTable with your datagridview i.e
C#
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]'
 
Share this answer
 
Comments
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();
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:
C#
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!
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900