grid

  1  using  System;
  2  using  System.Collections.Generic;
  3  using  System.ComponentModel;
  4  using  System.Windows.Forms;
  5  using  DevExpress.XtraGrid.Columns;
  6  using  DevExpress.XtraGrid.Views.Base;
  7  using  DevExpress.XtraGrid.Views.BandedGrid;
  8  using  DevExpress.XtraEditors.Repository;
  9 
 10  namespace  XtraGridDemo1
 11  {
 12       public   partial   class  Form1 : DevExpress.XtraEditors.XtraForm
 13      {
 14           public  Form1()
 15          {
 16              InitializeComponent();
 17              
 18               // 首先拖到窗体上一个GridControl,在表格上点击“Click here to change view”链接,在弹出菜单中选择“convert to”-->“AdvBandedGridView”。
 19              
 20              InitGrid();
 21          }
 22 
 23           /// 初始化表格
 24           private   void  InitGrid()
 25          {
 26 
 27               //  advBandedGridView1是表格上的默认视图,注意这里声明的是:BandedGridView
 28              BandedGridView view  =  advBandedGridView1  as  BandedGridView;
 29              
 30              view.BeginUpdate();  // 开始视图的编辑,防止触发其他事件
 31              view.BeginDataUpdate();  // 开始数据的编辑
 32 
 33              view.Bands.Clear();
 34 
 35               // 修改附加选项
 36              view.OptionsView.ShowColumnHeaders  =   false ;                          // 因为有Band列了,所以把ColumnHeader隐藏
 37              view.OptionsView.ShowGroupPanel  =   false ;                             // 如果没必要分组,就把它去掉
 38              view.OptionsView.EnableAppearanceEvenRow  =   false ;                    // 是否启用偶数行外观
 39              view.OptionsView.EnableAppearanceOddRow  =   true ;                      // 是否启用奇数行外观
 40              view.OptionsView.ShowFilterPanelMode  =  ShowFilterPanelMode.Never;    // 是否显示过滤面板
 41              view.OptionsCustomization.AllowColumnMoving  =   false ;                 // 是否允许移动列
 42              view.OptionsCustomization.AllowColumnResizing  =   false ;               // 是否允许调整列宽
 43              view.OptionsCustomization.AllowGroup  =   false ;                        // 是否允许分组
 44              view.OptionsCustomization.AllowFilter  =   false ;                       // 是否允许过滤
 45              view.OptionsCustomization.AllowSort  =   true ;                          // 是否允许排序
 46              view.OptionsSelection.EnableAppearanceFocusedCell  =   true ;            // ???
 47              view.OptionsBehavior.Editable  =   true ;                                // 是否允许用户编辑单元格
 48 
 49               // 添加列标题
 50              GridBand bandID  =  view.Bands.AddBand( " ID " );
 51              bandID.Visible  =   false // 隐藏ID列
 52              GridBand bandName  =  view.Bands.AddBand( " 姓名 " );
 53              GridBand bandSex  =  view.Bands.AddBand( " 性别 " );
 54              GridBand bandBirth  =  view.Bands.AddBand( " 出生日期 " );
 55              GridBand bandScore  =  view.Bands.AddBand( " 分数 " );
 56              GridBand bandMath  =  bandScore.Children.AddBand( " 数学 " );
 57              GridBand bandChinese  =  bandScore.Children.AddBand( " 语文 " );
 58              GridBand bandEnglish  =  bandScore.Children.AddBand( " 英语 " );
 59              GridBand bandSubTotal  =  bandScore.Children.AddBand( " 小计 " );
 60              GridBand bandRemark  =  view.Bands.AddBand( " 备注 " );
 61 
 62               // 列标题对齐方式
 63              bandName.AppearanceHeader.TextOptions.HAlignment  =  DevExpress.Utils.HorzAlignment.Center;
 64              bandSex.AppearanceHeader.TextOptions.HAlignment  =  DevExpress.Utils.HorzAlignment.Center;
 65              bandBirth.AppearanceHeader.TextOptions.HAlignment  =  DevExpress.Utils.HorzAlignment.Center;
 66              bandScore.AppearanceHeader.TextOptions.HAlignment  =  DevExpress.Utils.HorzAlignment.Center;
 67              bandMath.AppearanceHeader.TextOptions.HAlignment  =  DevExpress.Utils.HorzAlignment.Center;
 68              bandChinese.AppearanceHeader.TextOptions.HAlignment  =  DevExpress.Utils.HorzAlignment.Center;
 69              bandEnglish.AppearanceHeader.TextOptions.HAlignment  =  DevExpress.Utils.HorzAlignment.Center;
 70              bandSubTotal.AppearanceHeader.TextOptions.HAlignment  =  DevExpress.Utils.HorzAlignment.Center;
 71              bandRemark.AppearanceHeader.TextOptions.HAlignment  =  DevExpress.Utils.HorzAlignment.Center;
 72 
 73               // 模拟几个数据
 74              List < Record >  listDataSource  =   new  List < Record > ();
 75              listDataSource.Add( new  Record( 1 " 张三 " , " " ,Convert.ToDateTime( " 1989-5-6 " ) , 115.5f , 101 , 96 , "" ));
 76              listDataSource.Add( new  Record( 2 " 李四 " " " , Convert.ToDateTime( " 1987-12-23 " ),  92 85 87 "" ));
 77              listDataSource.Add( new  Record( 3 " 王五 " " " , Convert.ToDateTime( " 1990-2-11 " ),  88 69 41.5f "" ));
 78              listDataSource.Add( new  Record( 4 " 赵六 " " " , Convert.ToDateTime( " 1988-9-1 " ),  119 108 110 " 备注行 " ));
 79               // 绑定数据源并显示
 80              gridControl1.DataSource  =  listDataSource;
 81              gridControl1.MainView.PopulateColumns();
 82 
 83               // [小计]这一列因为没绑定数据源,所以需要手动添加
 84               // (有点复杂,慢慢看吧)
 85               string [] fieldNames  =   new   string [] {  " SubTotal "  };
 86              GridColumn column;    // 声明单列
 87              column  =  view.Columns.AddField(fieldNames[ 0 ]);   // 添加一个数据字段
 88              column.VisibleIndex  =  view.Columns.Count  - 1 ;   // 设置该列在编辑视图时的显示位置(倒数第二列)  
 89              column.UnboundType  =  DevExpress.Data.UnboundColumnType.Decimal;
 90              column.OptionsColumn.AllowEdit  =   false ;      // 此列不可编辑
 91              column.Visible  =   true ;
 92              view.Columns.Add(column);    // 视图中添加一列
 93 
 94               // 绑定事件,当[分数]改变时[小计]也跟着变
 95               // (好像只有在绑定事件里才能改变该单元格数值,直接修改无效)
 96              view.CustomUnboundColumnData  +=   new
 97                  DevExpress.XtraGrid.Views.Base.CustomColumnDataEventHandler(advBandedGridView1_CustomUnboundColumnData);
 98 
 99               // [性别]列绑定ComboBox
100              RepositoryItemComboBox riCombo  =   new  RepositoryItemComboBox();
101              riCombo.Items.AddRange( new   string [] { " " " " });
102              gridControl1.RepositoryItems.Add(riCombo);
103              view.Columns[ " Sex " ].ColumnEdit  =  riCombo;
104 
105               // [出生年月]列绑定Date
106              RepositoryItemDateEdit riDate  =   new  RepositoryItemDateEdit();
107              gridControl1.RepositoryItems.Add(riDate);
108              view.Columns[ " Birth " ].ColumnEdit  =  riDate;
109 
110               // [分数]列绑定SpinEdit
111              RepositoryItemSpinEdit riSpin  =   new  RepositoryItemSpinEdit();
112              gridControl1.RepositoryItems.Add(riSpin);
113              view.Columns[ " Math " ].ColumnEdit  =  riSpin;
114              view.Columns[ " Chinese " ].ColumnEdit  =  riSpin;
115              view.Columns[ " English " ].ColumnEdit  =  riSpin;
116 
117               // [备注]列绑定MemoExEdit
118              RepositoryItemMemoExEdit riMemoEx  =   new  RepositoryItemMemoExEdit();
119              gridControl1.RepositoryItems.Add(riMemoEx);
120              view.Columns[ " Remark " ].ColumnEdit  =  riMemoEx;
121 
122 
123               // 小计列添加汇总
124              view.OptionsView.ShowFooter  =   true ;      // 显示表格页脚
125              view.Columns[ " SubTotal " ].SummaryItem.FieldName  =   " SubTotal " ;
126              view.Columns[ " SubTotal " ].SummaryItem.DisplayFormat  =   " {0:f2} " ;
127              view.Columns[ " SubTotal " ].SummaryItem.SummaryType  =  DevExpress.Data.SummaryItemType.Average;
128 
129               // 将标题列和数据列对应
130              view.Columns[ " ID " ].OwnerBand  =  bandID;
131              view.Columns[ " Name " ].OwnerBand  =  bandName;
132              view.Columns[ " Sex " ].OwnerBand  =  bandSex;
133              view.Columns[ " Birth " ].OwnerBand  =  bandBirth;
134              view.Columns[ " Math " ].OwnerBand  =  bandMath;
135              view.Columns[ " Chinese " ].OwnerBand  =  bandChinese;
136              view.Columns[ " English " ].OwnerBand  =  bandEnglish;
137              view.Columns[ " SubTotal " ].OwnerBand  =  bandSubTotal;
138              view.Columns[ " Remark " ].OwnerBand  =  bandRemark;
139 
140              view.EndDataUpdate(); // 结束数据的编辑
141              view.EndUpdate();    // 结束视图的编辑
142 
143 
144          }
145 
146 
147 
148           //  计算小计
149           private   float  calcSubTotal( float  math,  float  chinese,  float  english)
150          {
151               return  math  +  chinese  +  english;
152          }
153 
154 
155           private   void  advBandedGridView1_CustomUnboundColumnData( object  sender, CustomColumnDataEventArgs e)
156          {
157              ColumnView colView  =  sender  as  ColumnView;
158               if  (e.Column.FieldName  ==   " SubTotal "   &&  e.IsGetData) e.Value  =  calcSubTotal(
159                       Convert.ToSingle(colView.GetRowCellValue(e.RowHandle, colView.Columns[ " Math " ])),
160                       Convert.ToSingle(colView.GetRowCellValue(e.RowHandle, colView.Columns[ " Chinese " ])),
161                       Convert.ToSingle(colView.GetRowCellValue(e.RowHandle, colView.Columns[ " English " ])));
162          }
163 
164 
165 
166           #region  运行时绑定到实现Ilist接口的数据源
167 
168           public   class  Record
169          {
170               int  id;
171              DateTime birth;
172               string  name, sex, remark;
173               float  math, chinese, english;
174               public  Record( int  id,  string  name,  string  sex, DateTime birth,  float  math,  float  chinese,  float  english,  string  remark)
175              {
176                   this .id  =  id;
177                   this .name  =  name;
178                   this .sex  =  sex;
179                   this .birth  =  birth;
180                   this .math  =  math;
181                   this .chinese  =  chinese;
182                   this .english  =  english;
183                   this .remark  =  remark;
184              }
185               public   int  ID {  get  {  return  id; } }
186               public   string  Name
187              {
188                   get  {  return  name; }
189                   set  { name  =  value; }
190              }
191               public   string  Sex
192              {
193                   get  {  return  sex; }
194                   set  { sex  =  value; }
195              }
196               public  DateTime Birth
197              {
198                   get  {  return  birth; }
199                   set  { birth  =  value; }
200              }
201               public   float  Math
202              {
203                   get  {  return  math; }
204                   set  { math  =  value; }
205              }
206               public   float  Chinese
207              {
208                   get  {  return  chinese; }
209                   set  { chinese  =  value; }
210              }
211               public   float  English
212              {
213                   get  {  return  english; }
214                   set  { english  =  value; }
215              }
216               public   string  Remark
217              {
218                   get  {  return  remark; }
219                   set  { remark  =  value; }
220              }
221 
222 
223          }
224 
225           #endregion
226 
227 
228      }
229  }
230 

 

转载于:https://www.cnblogs.com/slzfish/archive/2010/01/30/1660169.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值