Click here to Skip to main content
12,630,137 members (26,279 online)
Rate this:
 
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)
        {
            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 3-Jul-12 1:02am
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

Rate this: bad
 
good
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"];
grStudentAttend.DataBind();
  Permalink  
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.
UL UL ALBAB 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
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.161205.3 | Last Updated 3 Jul 2012
Copyright © CodeProject, 1999-2016
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