Click here to Skip to main content
15,889,867 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi friends,

This is my code for adding datatable value to gridview row by row. Here I cannot create new row for databind in asp.net. how can solve it.
DataTable bindclassattendance = inter.getstudentattendresult(comp);
           if (bindclassattendance.Rows.Count>0 )
           {

               TemplateField amtemp = new TemplateField();
               amtemp.ShowHeader = true;
               amtemp.HeaderText = "AM";
               amtemp.ItemTemplate = new gridviewtemplate(DataControlRowType.DataRow, "AM", "AM", "CheckBox");
               Classattendancegrid.Columns.Add(amtemp);

               TemplateField pmtemp = new TemplateField();
               pmtemp.ShowHeader = true;
               pmtemp.HeaderText = "PM";
               pmtemp.ItemTemplate = new gridviewtemplate(DataControlRowType.DataRow, "PM", "PM", "CheckBox");
               Classattendancegrid.Columns.Add(pmtemp);




               //Classattendancegrid.DataSource = bindclassattendance;
               //  Classattendancegrid.DataBind();
               for (int i = 0; i < bindclassattendance.Rows.Count; i++)
               {

                   Classattendancegrid.Rows[i].Cells[0].Text = bindclassattendance.Rows[i]["AdmissionNumber"].ToString();
                   Classattendancegrid.Rows[i].Cells[1].Text = bindclassattendance.Rows[i]["RollNumber"].ToString();
                   Classattendancegrid.Rows[i].Cells[2].Text = bindclassattendance.Rows[i]["Name"].ToString();
                   bool check1 = Convert.ToBoolean(bindclassattendance.Rows[i]["AM"].ToString());
                   if (check1)
                   {
                       CheckBox chk = Classattendancegrid.Rows[i].FindControl("AM") as CheckBox;
                       chk.Checked = true;

                   }
                   else
                   {
                       CheckBox chk = Classattendancegrid.Rows[i].FindControl("AM") as CheckBox;
                       chk.Checked = false;

                   }
                   bool check2 = Convert.ToBoolean(bindclassattendance.Rows[i]["PM"].ToString());
                   if (check2)
                   {
                       CheckBox chk = Classattendancegrid.Rows[i].FindControl("PM") as CheckBox;
                       chk.Checked = true;

                   }
                   else
                   {
                       CheckBox chk = Classattendancegrid.Rows[i].FindControl("PM") as CheckBox;
                       chk.Checked = false;

                   }

               }
Posted
Updated 23-Sep-13 20:20pm
v2

1 solution

Rather than adding rows manually why don't you bind the datatable directly to the datasource of the gridview with additional columns you are adding manually?

I would prefer to add additional columns in the datatable & will add checkbox using the item template column in the aspx page itself rather than writing on .cs page
 
Share this answer
 
Comments
baskaran chellasamy 24-Sep-13 3:44am    
gridview is bind dynamically with different columns based on the condition. so i cannot add the column in aspx page.

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