- How to make attendance sheet with gridview using C#?
- public void addnamestogrid()
- {
- date = Textdatetime.Text.Split('/');
- day = Convert.ToInt32(date[1].ToString());
- int dayscount = 0;
- if (date[0].ToString() == "4" || date[0].ToString() == "6" || date[0].ToString() == "9" || date[0].ToString() == "11")
- {
- dayscount = 30;
- }
- if (date[0].ToString() == "2")
- {
- int year = Convert.ToInt32(date[2].ToString());
- if (DateTime.IsLeapYear(year) != true)
- {
- dayscount = 28;
- }
- else
- {
- dayscount = 29;
- }
- }
- string connString = ConfigurationManager.ConnectionStrings["cn"].ConnectionString;
- SqlConnection conn = new SqlConnection(connString);
- string query1 = "select studentname from student where classid='" + Classid + "' and sectionid='" + Sectionid + "' ";
- SqlCommand cmd1 = new SqlCommand(query1, conn);
- conn.Open();
- dr1 = cmd1.ExecuteReader(CommandBehavior.CloseConnection);
- dt2.Load(dr1);
- dt2.DefaultView.Sort = "studentname";
- DataTable dt = new DataTable();
- dt.Columns.Add("StudentName");
- for (int i = 0; i < dt2.Rows.Count; i++)
- {
- DataRow oItem = dt.NewRow();
- oItem[0] = dt2.Rows[i][0].ToString();
- dt.Rows.Add(oItem);
- }
- BoundField nameColumn = new BoundField();
- nameColumn.DataField = "StudentName";
- nameColumn.HeaderText = "Student Name";
- GridView1.Columns.Insert(0, nameColumn);
- GridView gv = new GridView();
- GridView1.AutoGenerateColumns = false;
- for (int i = 1; i < dayscount+1; i++)
- {
- TemplateField TmpCol = new TemplateField();
- TmpCol.HeaderText = i.ToString();
- GridView1.Columns.Add(TmpCol);
- //TmpCol.ItemTemplate = new TemplateHandler();
- }
- ViewState["CurrentTable"] = dt;
- GridView1.DataSource = dt;
- GridView1.DataBind();
- dr1.Close();
- dr1.Dispose();
- Form.Controls.Add(gv);
- }
- protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
- {
- if (e.Row.RowType == DataControlRowType.DataRow)
- {
- for (int i = 1; i < GridView1.Columns.Count; i++)
- {
- TextBox txtQty = new TextBox();
- txtQty.ID = "cmd" + i + "";
- txtQty.Width = 20;
- txtQty.Text = "P";
- TableCell tc = e.Row.Cells[i];
- tc.Controls.Add(txtQty);
- e.Row.Cells.AddAt(i + 1, tc);
- }
- }