乱笔.................table控件,带一列合并

最近有一个项目遇到有合并一列的数据表格,试试DataGrid,当时可以通过ItemBound和ItemCreate事件中,通过单元格的合并方式来进行合并。
事件中的e.Item可以类型转换为 TableRow,但如果想通过 e.Item.Parent方式得到 table 是不可能的。

最终还是作了一个webcontrol,来完成下面的表格。



其中,无非是创建一些TableCell或者是TableRow,给出创建table body的代码
如果比较熟悉HTML编码,创建这些就不在话下了。

 1 
 2         #region 创建表格body
 3         private void CreateTableBody()
 4         {
 5             if(_ds ==null)
 6                 return;
 7             if(_ds.Tables.Count!=2)
 8                 return;
 9 
10 
11             DataTable groupTable = _ds.Tables[0];
12             DataTable resultTable = _ds.Tables[1];
13             for (int i=0;i<groupTable.Rows.Count;i++)
14             {
15                 String GroupRowValue = groupTable.Rows[i][this.GroupRowName].ToString();
16                 DataRow[] editionList= resultTable.Select(this.GroupRowName+"='"+GroupRowValue+"'");
17                 for(int j=0;j<editionList.Length;j++)
18                 {
19                     TableRow newRow = new TableRow();
20                     newRow.Attributes.Add("class","trbody");
21                     this.Controls.Add(newRow);
22                     TableCell GroupCell = new TableCell();
23                     if(j==0)
24                     {
25                         GroupCell.RowSpan = editionList.Length;
26                         GroupCell.Text = GroupRowValue;
27                         if(RowWidth[0]!="")
28                             GroupCell.Attributes.Add("width",RowWidth[0]+"px");
29                         newRow.Cells.Add(GroupCell);
30                     }
31                     for(int k=0;k<OtherRowName.Length;k++)
32                     {
33                         if(OtherRowName[k]!=GroupRowName)
34                         {
35                             TableCell newCell = new TableCell();
36                             newCell.Text = editionList[j][OtherRowName[k]].ToString();
37                             if(RowWidth[k]!="")
38                                 newCell.Attributes.Add("width",RowWidth[k]+"px");
39                             newRow.Cells.Add(newCell);
40                     
41                             newCell.RowSpan =1;
42                             if(OnBindTableBody!=null)
43                                 OnBindTableBody(newCell,OtherRowName[k],editionList[j]);
44 
45                             
46 
47                         }
48                     }
49 
50                 }
51 
52 
53             }
54             
55             
56 
57             
58 
59 
60 
61         }
62         #endregion

设计时候视图



原文地址:https://www.cnblogs.com/king_astar/p/266315.html