Lotus Pond

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::
这是GridView动态添加行的基本代码,可以进行修改扩充,实现自己想要的动态添加行效果。
Code
 1             //创建一个GridView的一个分隔行(根据DataControlRowType来设置)
 2             GridViewRow rowSeparator = new GridViewRow(00, DataControlRowType.Separator, DataControlRowState.Normal);
 3             //设置行的底色
 4             rowSeparator.BackColor = System.Drawing.Color.White;
 5             //设置单元格集
 6             //可以根据实际情况设置,我在这儿是根据RowDataBound事件(e参数)来设置的
 7             TableCellCollection cells = e.Row.Cells;
 8             //设置单元格,根据实际情况增加,我这儿是增加一个跨所有列的行
 9             TableCell separatorCell = new TableCell();
10             //根据GridView的第一列的显示情况设置单元格和跨列数
11             if (gvMain.Columns[0].Visible == true)
12             {
13                 separatorCell.ColumnSpan = cells.Count;
14             }
15             else
16             {
17                 separatorCell.ColumnSpan = cells.Count - 1;
18             }
19             //单元格的对齐
20             separatorCell.HorizontalAlign = HorizontalAlign.Right;
21             //单元格的背景色
22             separatorCell.BackColor = System.Drawing.Color.FromArgb(226226226);
23             //单元格的高度
24             separatorCell.ControlStyle.Height = 5;
25             //在单元格集中增加单元格控件
26             rowSeparator.Cells.Add(separatorCell);
27             //设置GridView行的可见性
28             rowSeparator.Visible = true;
29             //在GridView中的相应行插入行
30             gvMain.Controls[0].Controls.AddAt(e.Row.RowIndex + 1, rowSeparator);

 

这是在项目中的一个实例,是动态添加按照某一个属性来统计总数SubTotal的例子,可以参照下,有不明白的地方可以给我留言或者发表评论。
Code
 1         #region Insert GridView Row
 2 
 3         private void InsertSubTotalRow(int subTotalRow, int amountNumber, decimal totalAmount, int remindTextIndex, string remindText)
 4         {
 5             GridViewRow rowSeparator = new GridViewRow(00, DataControlRowType.Separator, DataControlRowState.Normal);
 6             //To add a data row
 7             //Set the background color of row
 8             rowSeparator.BackColor = System.Drawing.Color.White;
 9             //Setting the cell
10             TableCellCollection cells = gvwTemplate.Rows[0].Cells;
11 
12             //Add column
13             for (int i = 0; i < gvwTemplate.Rows[0].Cells.Count; i++)
14             {
15                 TableCell separatorCell = new TableCell();
16 
17                 //According to the first column of GridView to set the display of the cell and cross out the number
18                 if (gvwTemplate.Columns[i].Visible == true)
19                 {
20                     separatorCell.ColumnSpan = 0;
21 
22                     if (i == remindTextIndex)
23                     {
24                         Label lblRemindText = new Label();
25 
26                         if (remindText == "")
27                         {
28                             lblRemindText.Text = "";
29                         }
30                         else
31                         {
32                             lblRemindText.Text = "<nobr>" + remindText + "</nobr>";
33                         }
34 
35                         lblRemindText.Style.Add("font-weight""700");
36                         lblRemindText.Style.Add("height""25px");
37                         separatorCell.Controls.Add(lblRemindText);
38                         //Cell alignment
39                         separatorCell.HorizontalAlign = HorizontalAlign.Left;
40 
41                     }
42 
43                     if (i == amountNumber)
44                     {
45                         Label lblAmount = new Label();
46                         if (totalAmount != 0)
47                         {
48                             lblAmount.Text = "<div style='width:140px;text-align:right;font-weight:700;'>" + Format.FormatAmount(totalAmount) + "</div>";
49                         }
50                         else
51                         {
52                             lblAmount.Text = "";
53                         }
54                         lblAmount.Style.Add("height""25px");
55                         separatorCell.Controls.Add(lblAmount);
56 
57                         //Cell alignment
58                         separatorCell.HorizontalAlign = HorizontalAlign.Right;
59                     }
60 
61                     //The cell background color
62 
63                     separatorCell.BackColor = System.Drawing.Color.FromName("#ccccee");
64                     //The height of the cell
65                     separatorCell.ControlStyle.Height = 25;
66 
67                     //An increase in cell concentration of the cell control
68                     rowSeparator.Cells.Add(separatorCell);
69 
70                 }
71             }
72 
73             total = 0;
74 
75             //Set the visibility of GridView row
76             rowSeparator.Visible = true;
77 
78             //In the corresponding row in the GridView insert row
79             gvwTemplate.Controls[0].Controls.AddAt(subTotalRow, rowSeparator);
80 
81         }
82 
83         #endregion

 

posted on 2010-01-21 12:45  Lotus Pond  阅读(1375)  评论(0编辑  收藏  举报