Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jun 17th, 2012  |  syntax: None  |  size: 2.37 KB  |  hits: 19  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. How to make attendance sheet with gridview using C#?
  2. public void addnamestogrid()
  3.     {
  4.     date = Textdatetime.Text.Split('/');
  5.     day = Convert.ToInt32(date[1].ToString());
  6.     int dayscount = 0;
  7.  
  8.     if (date[0].ToString() == "4" || date[0].ToString() == "6" || date[0].ToString() == "9" || date[0].ToString() == "11")
  9.     {
  10.       dayscount = 30;
  11.  
  12.     }
  13.     if (date[0].ToString() == "2")
  14.     {
  15.       int year = Convert.ToInt32(date[2].ToString());
  16.  
  17.       if (DateTime.IsLeapYear(year) != true)
  18.       {
  19.       dayscount = 28;
  20.       }
  21.       else
  22.       {
  23.       dayscount = 29;
  24.       }
  25.     }
  26.  
  27.  
  28.  
  29.     string connString = ConfigurationManager.ConnectionStrings["cn"].ConnectionString;
  30.     SqlConnection conn = new SqlConnection(connString);
  31.  
  32.     string query1 = "select studentname from student where classid='" + Classid + "' and sectionid='" + Sectionid + "' ";
  33.     SqlCommand cmd1 = new SqlCommand(query1, conn);
  34.     conn.Open();
  35.     dr1 = cmd1.ExecuteReader(CommandBehavior.CloseConnection);
  36.     dt2.Load(dr1);
  37.     dt2.DefaultView.Sort = "studentname";
  38.  
  39.     DataTable dt = new DataTable();
  40.  
  41.     dt.Columns.Add("StudentName");
  42.  
  43.     for (int i = 0; i < dt2.Rows.Count; i++)
  44.     {
  45.  
  46.       DataRow oItem = dt.NewRow();
  47.       oItem[0] = dt2.Rows[i][0].ToString();
  48.  
  49.       dt.Rows.Add(oItem);
  50.  
  51.     }
  52.     BoundField nameColumn = new BoundField();
  53.     nameColumn.DataField = "StudentName";
  54.     nameColumn.HeaderText = "Student Name";
  55.     GridView1.Columns.Insert(0, nameColumn);
  56.  
  57.     GridView gv = new GridView();
  58.     GridView1.AutoGenerateColumns = false;
  59.  
  60.     for (int i = 1; i < dayscount+1; i++)
  61.     {
  62.       TemplateField TmpCol = new TemplateField();
  63.       TmpCol.HeaderText = i.ToString();
  64.  
  65.       GridView1.Columns.Add(TmpCol);
  66.       //TmpCol.ItemTemplate = new TemplateHandler();
  67.     }
  68.  
  69.     ViewState["CurrentTable"] = dt;
  70.  
  71.     GridView1.DataSource = dt;
  72.     GridView1.DataBind();
  73.  
  74.  
  75.     dr1.Close();
  76.     dr1.Dispose();
  77.  
  78.     Form.Controls.Add(gv);
  79.  
  80.     }
  81.  
  82.  protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
  83.     {
  84.  
  85.  
  86.     if (e.Row.RowType == DataControlRowType.DataRow)
  87.     {
  88.  
  89.       for (int i = 1; i < GridView1.Columns.Count; i++)
  90.       {
  91.       TextBox txtQty = new TextBox();
  92.       txtQty.ID = "cmd" + i + "";
  93.       txtQty.Width = 20;      
  94.       txtQty.Text = "P";        
  95.       TableCell tc = e.Row.Cells[i];
  96.       tc.Controls.Add(txtQty);
  97.  
  98.       e.Row.Cells.AddAt(i + 1, tc);
  99.       }
  100.  
  101.     }