GridView 动态 添加新行

这是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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在使用DevExpress GridControl控件中,如果需要添加新行,可以通过以下步骤实现: 1. 首先,在GridControl控件中选中想要添加新行的数据源,比如DataTable、List等。 2. 然后,在GridControl控件的Designer视图中设置控件的AllowAddNewRow属性为True。 3. 接着,在代码中调用GridView控件的AddNewRow方法,启动添加新行操作。 4. 在GridView控件的InitNewRow事件中,对新行进行初始化,比如设置默认值等。 5. 最后,将新行添加到数据源中,刷新GridView的数据显示。 下面是示例代码: 1. 在GridControl控件的Designer视图中设置AllowAddNewRow属性为True: ![image](https://user-images.githubusercontent.com/8575679/137455672-6dfeefa6-0ed6-4c6b-8d1e-0cfa34dde057.png) 2. 在代码中调用GridView的AddNewRow方法启动添加新行操作: ``` gridView1.AddNewRow(); ``` 3. 在GridView的InitNewRow事件中进行新行的初始化: ``` private void gridView1_InitNewRow(object sender, DevExpress.XtraGrid.Views.Grid.InitNewRowEventArgs e) { gridView1.SetRowCellValue(e.RowHandle, "ID", 0); gridView1.SetRowCellValue(e.RowHandle, "Name", "New Row"); } ``` 这里通过SetRowCellValue方法设置新行的ID和Name属性值。 4. 最后,在代码中将新行添加到数据源中,刷新GridView的数据显示: ``` dataTable.Rows.Add(gridView1.GetDataRow(gridView1.FocusedRowHandle)); gridView1.RefreshData(); ``` 这里通过Add方法将新行添加到DataTable中,然后调用GridView的RefreshData方法刷新数据显示。 以上就是在DevExpress GridControl控件中添加新行的步骤,实现起来相对简单。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值