说明:
最好是通过编辑视图进行设计,后台编码有点麻烦。
例图:(上面的GC是后台编写 ,下面的是设计器设计)
后台代码编写:
public void InitCtrl() {
DevExpress.XtraGrid.Views.BandedGrid.BandedGridView view = bgv as DevExpress.XtraGrid.Views.BandedGrid.BandedGridView;
//开始视图的编辑
view.BeginUpdate();
//开始数据的编辑
view.BeginDataUpdate();
view.Bands.Clear(); //因为有band列了,隐藏列标题
view.OptionsView.ShowColumnHeaders = false; //列标题
DevExpress.XtraGrid.Views.BandedGrid.GridBand bandID = view.Bands.AddBand("Stu_ID");
bandID.Visible = false;
DevExpress.XtraGrid.Views.BandedGrid.GridBand bandNnum = view.Bands.AddBand("学号");
DevExpress.XtraGrid.Views.BandedGrid.GridBand bandName = view.Bands.AddBand("姓名");
//合并表头显示的标题
DevExpress.XtraGrid.Views.BandedGrid.GridBand bandSource = view.Bands.AddBand("详细信息");
DevExpress.XtraGrid.Views.BandedGrid.GridBand bandSex = bandSource.Children.AddBand("性别");
DevExpress.XtraGrid.Views.BandedGrid.GridBand bandAge = bandSource.Children.AddBand("年龄");
DevExpress.XtraGrid.Views.BandedGrid.GridBand bandBirth = bandSource.Children.AddBand("生日"); DevExpress.XtraGrid.Views.BandedGrid.GridBand bandAdd = view.Bands.AddBand("地址");
//合并表头居中显示
bandSource.AppearanceHeader.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Center; //是否显示水平滚动条
view.OptionsView.ColumnAutoWidth = true;
// panel 值
// view.GroupPanelText = "学生信息"; //因为有Band列了,所以把ColumnHeader隐藏
view.OptionsView.ShowColumnHeaders = false;
view.OptionsView.ShowGroupPanel = false; //列绑定
string[] str = new string[] { "Stu_ID", "Stu_Number", "Stu_Name", "Stu_Sex", "Stu_Age", "Stu_BirthDate", "Stu_Address" };
for (int i = 0; i < str.Length; i++) {
DevExpress.XtraGrid.Views.BandedGrid.BandedGridColumn col = view.Columns.AddField(str[i]);
//设置该列在编辑视图时的显示位置(倒数第二列)
col.VisibleIndex = i;
//设置列格式
// col.UnboundType = DevExpress.Data.UnboundColumnType.Decimal;
//此列不可编辑
col.OptionsColumn.AllowEdit = false;
col.Visible = true;
//列内容居中
col.AppearanceCell.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Center;
//视图中添加一列
view.Columns.Add(col);
} //列匹配
view.Columns["Stu_ID"].OwnerBand = bandID;
view.Columns["Stu_Number"].OwnerBand = bandNnum;
view.Columns["Stu_Name"].OwnerBand = bandName;
view.Columns["Stu_Sex"].OwnerBand = bandSex;
view.Columns["Stu_Age"].OwnerBand = bandAge;
view.Columns["Stu_BirthDate"].OwnerBand = bandBirth;
view.Columns["Stu_Address"].OwnerBand = bandAdd; //自适应宽度
view.BestFitColumns(); view.EndDataUpdate();
view.EndUpdate();
}
版权声明:本文为博主原创文章,未经博主允许不得转载。