动态添加datagrid里模板列的每一行数据,并保存在dataset中...


在模板列里动态填充每一行,然后加入到dataset中。。。
1.代码的最后dataset ds用了session保存,这样的做法只限测试之用,若要在实际中请慎用哈,o(∩_∩)o...~
2.服务器一般只用session保存一些用户登陆信息的id等,对于象这样的ds会占用宝贵的服务器资源,
不推荐使用o(∩_∩)o...
3.session 很强大,后果很严重o(∩_∩)o...~
4.在select 语句中用了union填充集合并返回,注意:集合必须具有相同的属性才可用UNION关键词。

ContractedBlock.gif ExpandedBlockStart.gif 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         

转载于:https://www.cnblogs.com/junyistar/archive/2007/06/01/767813.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值