DataGrid绑定两个DataTable
项目要统计从内网、外网的信息数据,通过WebService查询出外网的数据后,与内网的数据一起绑定到一个DataGrid,
在网上搜索了一下,答案马上就出来了
1 BusinessFacade.TestQuestion TQ = new TestQuestion ();
2 DataTable table = new DataTable ();
3 table.Columns .Add ("Position",typeof(string));
4 DwService.Service service = new Service ();
5 DataSet inner= TQ.TestUserGetAll (testpageid,Functions.ToDateTime(starttime),Functions.ToDateTime (endtime) );
6 if(inner!=null && inner.Tables [0].Rows .Count >0)
7 {
8 DataTable innertable = inner.Tables [0];
9 innertable.Columns .Add ("Position",typeof(string));
10 for(int m=0;m<innertable.Rows .Count ;m++)
11 {
12 DataRow row = innertable.Rows [m];
13 row["Position"] = "内网";
14 }
15 table = innertable;
16 }
17 DataSet outer = service.TestUserGetAll (testpageid,Functions.ToDateTime(starttime),Functions.ToDateTime (endtime) );
18 if(outer!=null && outer.Tables [0].Rows.Count >0)
19 {
20 DataTable outtable = outer.Tables [0];
21 outtable.Columns .Add ("Position",typeof(string));
22 DataRow dr;
23 for(int i=0;i<outtable.Rows .Count ;i++)
24 {
25 outtable.Rows [i]["Position"] = "外网";
26 dr = table.NewRow ();
27 for(int j=0;j<table.Columns.Count ;j++)
28 {
29 dr[j] = outtable.Rows [i][j];
30 }
31 table.Rows .Add (dr );
32 }
33 }
34 if(table!=null && table.Rows .Count >0)
35 {
36 DataView daview = table.DefaultView ;
37 daview.Sort = "starttime desc";
38 ViewState["table"] = table;
39
40 dgTestUser.DataSource = table;
41 dgTestUser.DataBind ();
45
46 }
把两个DataTable组合成一个后,用DataView对DataGrid进行排序。