GridView 动态 添加新行

news/2024/7/3 6:32:23
这是GridView动态添加行的基本代码,可以进行修改扩充,实现自己想要的动态添加行效果。
ExpandedBlockStart.gif Code
 1               // 创建一个GridView的一个分隔行(根据DataControlRowType来设置)
 2              GridViewRow rowSeparator  =   new  GridViewRow( 0 0 , 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( 226 226 226 );
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的例子,可以参照下,有不明白的地方可以给我留言或者发表评论。
ExpandedBlockStart.gif 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( 0 0 , 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

 

转载于:https://www.cnblogs.com/leospace/archive/2010/01/21/1653211.html


http://www.niftyadmin.cn/n/1493284.html

相关文章

类别不平衡问题

详解类别不平衡问题 卢总-类别不平衡问题的方法汇总 文章目录从多数类别中删除数据&#xff08;ENN、Tomeklink、NearMiss&#xff09;ENNNearMiss为少数类生成新样本&#xff08;SMOTE、Borderline-SMOTE、ADASYN&#xff09;集成方法EasyEnsemble算法BalanceCascade算法算法…

ORACLE函数大全(CSDN)

SQL中的单记录函数1.ASCII返回与指定的字符对应的十进制数;SQL> select ascii(A) A,ascii(a) a,ascii(0) zero,ascii( ) space from dual; A A ZERO SPACE--------- --------- --------- --------- 65 97 48 32 2.CHR给出整数,…

特征选择方法汇总

卢总 - 特征选择方法汇总 特征选择三种方法&#xff1a; Filter(过滤法) Wrapper(包装法) Embedded(嵌入法) 过滤法 卡方检验 直接看sklearn代码&#xff1a; 首先做OHE Y LabelBinarizer().fit_transform(y)做完之后YYY的shape是NKN\times KNK observed safe_spar…

[linux]cp和mv对文件和链接影响的区别

好久不写blog了。。。加油&#xff0c;继续写blog。今天和同事讨论到一个问题&#xff0c;问题描述&#xff1a; 有文件a&#xff0c;a1为其硬链接&#xff0c;即&#xff1a; ln a a1 现在有文件b&#xff0c;对其进行以下两种操作&#xff1a; 1. mv b a 2. cp b a 问这两种操…

【机器学习】【操作系统】【网络】【算法与数据结构】知识汇总

机器学习 L1不可导的时候该怎么办 操作系统 堆栈区别 堆和栈的区别&#xff1a; 一、堆栈空间分配区别&#xff1a; 1&#xff09;、栈&#xff08;操作系统&#xff09;&#xff1a;由操作系统自动分配释放 &#xff0c;存放函数的参数值&#xff0c;局部变量的值等。其…

记录tomcat服务器开启关闭时间

1、IO流 package com.zy.exercise; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import javax.servl…

graph embedding源码阅读

文章目录LINEstruc2vec计算结构距离计算每个结点的有序度序列遍历边表的顶点&#xff0c;每个都计算有序度序列LINE 看到 ge.models.line.LINE#_gen_sampling_table node_degree 表示出度 归一化后&#xff0c;得到顶点的分布norm_prob&#xff0c;然后算alias_table 构建al…

ORM到底如何使用了?

公司最近一个项目用到了实体类&#xff0c;借助其他成熟的框架实现了orm&#xff0c;可是在使用过程中却对于多表查询支持并不好。nbear看上去orm挺不错的&#xff0c;有空得研究下看看代码转载于:https://www.cnblogs.com/ocean2000/archive/2007/06/13/781755.html