在模板列里动态填充每一行,然后加入到dataset中。。。
1.代码的最后dataset ds用了session保存,这样的做法只限测试之用,若要在实际中请慎用哈,o(∩_∩)o...~
2.服务器一般只用session保存一些用户登陆信息的id等,对于象这样的ds会占用宝贵的服务器资源,
不推荐使用o(∩_∩)o...
3.session 很强大,后果很严重o(∩_∩)o...~
4.在select 语句中用了union填充集合并返回,注意:集合必须具有相同的属性才可用UNION关键词。
dynamicBind
1 ds1 = new DataSet();
2 sda1.Fill(ds1,"tj");
3 this.DataGrid1.DataSource = ds1.Tables["tj"];
4
5
6 string str = "select distinct(empID) from attender";
7
8 DataTable dt = new DataTable();
9 SqlDataAdapter ss = new SqlDataAdapter(str,con);
10 ss.Fill(dt);
11
12 int i = 0;
13
14
15 ds1.Tables["tj"].Columns.Add("迟到",typeof(string));
16
17 ds1.Tables["tj"].Columns.Add("加班",typeof(string));
18
19 ds1.Tables["tj"].Columns.Add("请假",typeof(string));
20
21 ds1.Tables["tj"].Columns.Add("休假",typeof(string));
22
23 ds1.Tables["tj"].Columns.Add("正常出勤",typeof(string));
24
25 this.DataGrid1.DataBind();
26
27 foreach(System.Web.UI.WebControls.DataGridItem dl in this.DataGrid1.Items)
28 {
29
30
31 string sqlstr = "select count(*), '正常出勤' as tt from attender,employees where attender.empID = '"+Convert.ToInt32(dt.Rows[i][0])+"' and attender.empID=employees.EmpID and attender.attendDate>='"+this.TextBox1.Text+"' and attender.attendDate<='"+this.TextBox2.Text+"' and attender.state='正常出勤' union select count(*), '休假' as tt from attender,employees where attender.empID = '"+Convert.ToInt32(dt.Rows[i][0])+"' and attender.empID=employees.EmpID and attender.attendDate>='"+this.TextBox1.Text+"' and attender.attendDate<='"+this.TextBox2.Text+"' and attender.state='休假' union select count(*), '请假' as tt from attender,employees where attender.empID = '"+Convert.ToInt32(dt.Rows[i][0])+"' and attender.empID=employees.EmpID and attender.attendDate>='"+this.TextBox1.Text+"' and attender.attendDate<='"+this.TextBox2.Text+"' and attender.state='请假' union select count(*), '加班' as tt from attender,employees where attender.empID = '"+Convert.ToInt32(dt.Rows[i][0])+"' and attender.empID=employees.EmpID and attender.attendDate>='"+this.TextBox1.Text+"' and attender.attendDate<='"+this.TextBox2.Text+"' and attender.state='加班' union select count(*), '迟到' as tt from attender,employees where attender.empID = '"+Convert.ToInt32(dt.Rows[i][0])+"' and attender.empID=employees.EmpID and attender.attendDate>='"+this.TextBox1.Text+"' and attender.attendDate<='"+this.TextBox2.Text+"' and attender.state='迟到' order by tt";
32 SqlCommand cmd = new SqlCommand(sqlstr,con);
33 SqlDataReader rs = cmd.ExecuteReader();
34
35 Label zccq = (Label)dl.FindControl("zccq");
36 Label qj = (Label)dl.FindControl("qj");
37 Label xj = (Label)dl.FindControl("xj");
38 Label jb = (Label)dl.FindControl("jb");
39 Label cd = (Label)dl.FindControl("cd");
40
41
42
43 rs.Read();
44 string cdd = Convert.ToString(rs.GetInt32(0));
45 ds1.Tables["tj"].Rows[i][5]=cdd;
46 cd.Text = cdd.ToString();
47
48
49 rs.Read();
50 string jbb = Convert.ToString(rs.GetInt32(0));
51 ds1.Tables["tj"].Rows[i][6]=jbb;
52 jb.Text = jbb.ToString();
53
54 rs.Read();
55 string qjj = Convert.ToString(rs.GetInt32(0));
56 ds1.Tables["tj"].Rows[i][7]=qjj;
57 qj.Text = qjj.ToString();
58
59 rs.Read();
60 string xjj = Convert.ToString(rs.GetInt32(0));
61 ds1.Tables["tj"].Rows[i][8]=xjj;
62 xj.Text = xjj.ToString();
63
64 rs.Read();
65 string zccqq= Convert.ToString(rs.GetInt32(0));
66 ds1.Tables["tj"].Rows[i][9]=zccqq;
67 zccq.Text = zccqq.ToString();
68
69
70
71 rs.Close();
72 if(i<dt.Rows.Count-1)
73 {
74 i++;
75 }
76 }
77
78
79 Session["dsssTable"]=ds1;
80
1 ds1 = new DataSet();
2 sda1.Fill(ds1,"tj");
3 this.DataGrid1.DataSource = ds1.Tables["tj"];
4
5
6 string str = "select distinct(empID) from attender";
7
8 DataTable dt = new DataTable();
9 SqlDataAdapter ss = new SqlDataAdapter(str,con);
10 ss.Fill(dt);
11
12 int i = 0;
13
14
15 ds1.Tables["tj"].Columns.Add("迟到",typeof(string));
16
17 ds1.Tables["tj"].Columns.Add("加班",typeof(string));
18
19 ds1.Tables["tj"].Columns.Add("请假",typeof(string));
20
21 ds1.Tables["tj"].Columns.Add("休假",typeof(string));
22
23 ds1.Tables["tj"].Columns.Add("正常出勤",typeof(string));
24
25 this.DataGrid1.DataBind();
26
27 foreach(System.Web.UI.WebControls.DataGridItem dl in this.DataGrid1.Items)
28 {
29
30
31 string sqlstr = "select count(*), '正常出勤' as tt from attender,employees where attender.empID = '"+Convert.ToInt32(dt.Rows[i][0])+"' and attender.empID=employees.EmpID and attender.attendDate>='"+this.TextBox1.Text+"' and attender.attendDate<='"+this.TextBox2.Text+"' and attender.state='正常出勤' union select count(*), '休假' as tt from attender,employees where attender.empID = '"+Convert.ToInt32(dt.Rows[i][0])+"' and attender.empID=employees.EmpID and attender.attendDate>='"+this.TextBox1.Text+"' and attender.attendDate<='"+this.TextBox2.Text+"' and attender.state='休假' union select count(*), '请假' as tt from attender,employees where attender.empID = '"+Convert.ToInt32(dt.Rows[i][0])+"' and attender.empID=employees.EmpID and attender.attendDate>='"+this.TextBox1.Text+"' and attender.attendDate<='"+this.TextBox2.Text+"' and attender.state='请假' union select count(*), '加班' as tt from attender,employees where attender.empID = '"+Convert.ToInt32(dt.Rows[i][0])+"' and attender.empID=employees.EmpID and attender.attendDate>='"+this.TextBox1.Text+"' and attender.attendDate<='"+this.TextBox2.Text+"' and attender.state='加班' union select count(*), '迟到' as tt from attender,employees where attender.empID = '"+Convert.ToInt32(dt.Rows[i][0])+"' and attender.empID=employees.EmpID and attender.attendDate>='"+this.TextBox1.Text+"' and attender.attendDate<='"+this.TextBox2.Text+"' and attender.state='迟到' order by tt";
32 SqlCommand cmd = new SqlCommand(sqlstr,con);
33 SqlDataReader rs = cmd.ExecuteReader();
34
35 Label zccq = (Label)dl.FindControl("zccq");
36 Label qj = (Label)dl.FindControl("qj");
37 Label xj = (Label)dl.FindControl("xj");
38 Label jb = (Label)dl.FindControl("jb");
39 Label cd = (Label)dl.FindControl("cd");
40
41
42
43 rs.Read();
44 string cdd = Convert.ToString(rs.GetInt32(0));
45 ds1.Tables["tj"].Rows[i][5]=cdd;
46 cd.Text = cdd.ToString();
47
48
49 rs.Read();
50 string jbb = Convert.ToString(rs.GetInt32(0));
51 ds1.Tables["tj"].Rows[i][6]=jbb;
52 jb.Text = jbb.ToString();
53
54 rs.Read();
55 string qjj = Convert.ToString(rs.GetInt32(0));
56 ds1.Tables["tj"].Rows[i][7]=qjj;
57 qj.Text = qjj.ToString();
58
59 rs.Read();
60 string xjj = Convert.ToString(rs.GetInt32(0));
61 ds1.Tables["tj"].Rows[i][8]=xjj;
62 xj.Text = xjj.ToString();
63
64 rs.Read();
65 string zccqq= Convert.ToString(rs.GetInt32(0));
66 ds1.Tables["tj"].Rows[i][9]=zccqq;
67 zccq.Text = zccqq.ToString();
68
69
70
71 rs.Close();
72 if(i<dt.Rows.Count-1)
73 {
74 i++;
75 }
76 }
77
78
79 Session["dsssTable"]=ds1;
80