XGrid绑定(转)

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