Click here to Skip to main content
11,574,503 members (67,207 online)
Rate this: bad
Please Sign up or sign in to vote.
See more: C#2.0 C# C#4.0
My Code:
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
                //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");
                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)

Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
Posted 3-Jul-12 0:02am
dimpledevani at 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

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

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

grStudentAttend.DataSource = db.dataSet.Tables["student"];
Vani Kulkarni at 3-Jul-12 6:16am
Correct! 5!
Rahul Rajat Singh at 3-Jul-12 6:21am
UL UL ALBAB at 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....
UL UL ALBAB at 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 at 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.
UL UL ALBAB at 3-Jul-12 6:36am
Its' pretty. But, I can't do it. Cause, I have added 3 columns manually in datagridview. So, I have to add data instance of those column.

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

  Print Answers RSS

Advertise | Privacy | Mobile
Web03 | 2.8.150624.2 | Last Updated 3 Jul 2012
Copyright © CodeProject, 1999-2015
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