Click here to Skip to main content
15,868,051 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
My Code:
C#
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                //DataTable dt = new DataTable();
                
               
                db.Adapter("select distinct StudentName, Roll from StudentAttendance where Class = '" + comClass.Text.ToString() + "' and Shift ='" + cmShift.Text.ToString() + "' and Section = '" +cmSection.Text.ToString()+ "' and YearAt = '" + dateTimeYear.Text.ToString() + "'","student");
                //MessageBox.Show(db.dataSet.Tables["student"].Rows.Count.ToString());
                for (int i = 0; i < db.dataSet.Tables["student"].Rows.Count; i++)
                {
                    grStudentAttend.Rows[i].Cells[0].Value = db.dataSet.Tables["student"].Rows[i][0].ToString();
                    grStudentAttend.Rows[i].Cells[1].Value = db.dataSet.Tables["student"].Rows[i][1].ToString();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

        }


Exception:
Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
Posted
Comments
dimpledevani 3-Jul-12 6:08am    
Why have you used a loop?? There is a datasource property of gridview where you can directly assign your datatable

1 solution

Why are you doing this. use the datasource property instead. try this

C#
grStudentAttend.DataSource = db.dataSet.Tables["student"];
grStudentAttend.DataBind();
 
Share this answer
 
Comments
Vani Kulkarni 3-Jul-12 6:16am    
Correct! 5!
Rahul Rajat Singh 3-Jul-12 6:21am    
thanks.
UL UL ALBAB 3-Jul-12 6:31am    
No, cause I have add 3 Column in datagridview. One of them have a checkbox too. Thus why, I have to do this....
Thanks...
UL UL ALBAB 3-Jul-12 6:26am    
Getting Error:
Error 1 'System.Windows.Forms.DataGridView' does not contain a definition for 'DataBind' and no extension method 'DataBind' accepting a first argument of type 'System.Windows.Forms.DataGridView' could be found (are you missing a using directive or an assembly reference?) F:\712012\MailTest\MailTest\Student\StudentAttendence.cs 40 33 MailTest
Rahul Rajat Singh 3-Jul-12 6:28am    
So it is a winform application. skip the databind and see if it works or not. I mean delete the line with databind in it. Tell me if it works.

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