代码如下:
public class DXGridControlHelper
{
/// <summary>
/// 获取显示指定列的GridView
/// </summary>
/// <param name="grid"></param>
/// <param name="columns">列集合</param>
/// <returns></returns>
public static DevExpress.XtraGrid.Views.Grid.GridView GetColumnsView(DevExpress.XtraGrid.GridControl grid,
List<GridColumnModel> columns)
{
DevExpress.XtraGrid.Views.Grid.GridView view = new DevExpress.XtraGrid.Views.Grid.GridView(grid); foreach (GridColumnModel item in columns)
{
DevExpress.XtraGrid.Columns.GridColumn column = new DevExpress.XtraGrid.Columns.GridColumn();
column.FieldName = item.DataColumnName;
column.Name = item.ColumnName;
column.Caption = item.ColumnTitle;
column.Visible = item.Visible;
view.Columns.Add(column);
}
//下面指定分组,-1 为不分组 0,1,2....为分组顺序 (add的时候指定groupindex 无效,可能为列未在视图中初始化)
for (int i = ; i < columns.Count; i++)
{
view.Columns[i].GroupIndex =columns[i].GroupIndex;
}
return view;
}
///<summary>
/// 设置按钮列
/// </summary>
/// <param name="dataColumnName">数据字段名</param>
/// <param name="dataColumnName">数据字段名</param>
/// <param name="btnText">按钮文本</param>
/// <param name="skinName">按钮皮肤名</param>
/// <param name="image">按钮图片</param>
/// <param name="location">图片位置</param>
/// <param name="clickFunction">按钮Handler</param>
/// <param name="view">basevew</param>
/// <param name="viewType">viewtype</param>
public static void SetButtonColumn(
string dataColumnName, string btnText, string skinName, System.Drawing.Image image,
DevExpress.XtraEditors.ImageLocation location,
DevExpress.XtraEditors.Controls.ButtonPressedEventHandler clickFunction, DevExpress.XtraGrid.Views.Base.BaseView view, ViewType viewType)
{
RepositoryItemButtonEdit rib = new RepositoryItemButtonEdit(); rib.TextEditStyle = DevExpress.XtraEditors.Controls.TextEditStyles.HideTextEditor;
rib.Buttons[].Kind = DevExpress.XtraEditors.Controls.ButtonPredefines.Glyph;
rib.ButtonClick += clickFunction;
rib.Buttons[].Caption = btnText; if(skinName!="")
rib.LookAndFeel.SkinName = skinName; rib.LookAndFeel.UseDefaultLookAndFeel = false; if (image != null)
{
rib.Buttons[].Image = image;
rib.Buttons[].ImageLocation = location;
} rib.Buttons[].Visible = true; switch (viewType)
{
case ViewType.GridView:
(view as DevExpress.XtraGrid.Views.Grid.GridView).Columns[dataColumnName].ColumnEdit = rib;
break;
case ViewType.BandView:
(view as DevExpress.XtraGrid.Views.BandedGrid.BandedGridView).Columns[dataColumnName].ColumnEdit = rib;
break;
default:
break;
}
}
} public enum ViewType
{
GridView,
BandView
}
} /// <summary>
/// 动态创建gridView视图,列的Model
/// </summary>
public class GridColumnModel
{
/// <summary>
/// 列表头文本
/// </summary>
public string ColumnTitle { get; set; } /// <summary>
/// 列唯一标识 name,默认为(column_sql语句中的字段名(小写))
/// column_sqlcolumnname
/// </summary>
private string _columnName;
public string ColumnName
{
get
{
if (string.IsNullOrEmpty(_columnName))
return "column_" + DataColumnName.ToLower();
return _columnName;
}
set { _columnName = value; }
} /// <summary>
/// sql 语句中的字段名称
/// </summary>
public string DataColumnName { get; set; } /// <summary>
/// 可见性,默认true
/// </summary>
private bool? _visible; public bool Visible
{
get
{
if (_visible == null)
return true;
return bool.Parse(_visible.ToString());
}
set
{
_visible = value;
}
} /// <summary>
/// 分组,默认-1
/// </summary>
private int? _groupIndex;
public int GroupIndex
{
get
{
if (_groupIndex == null)
{
_groupIndex = -;
}
return int.Parse(_groupIndex.ToString());
}
set
{
_groupIndex = value;
}
} }
使用该代码 方法:
说明:动态制定的列 要与数据源中的列字段名(sql语句中的字段名) 一致
var view2 = DXGridControlHelper.GetColumnsView(gridControl1, new List<ColumnModel>() {
new ColumnModel(){ColumnTitle="ID",DataColumnName="Id"},//默认该列为显示,列名为column_id
new ColumnModel(){ColumnTitle="课件名称",GroupIndex=,DataColumnName="Name"},//按照该列分组
new ColumnModel(){ColumnTitle="描述",DataColumnName="Description"},
new ColumnModel(){ColumnTitle="归属",DataColumnName="Objective"},
new ColumnModel(){ColumnTitle="图片",ColumnName="img",DataColumnName="ImgGif",Visible=false}
});
DataTable tab = cl.GetDataTable("select * from table_name");
gridControl1.MainView = view2;
gridControl1.DataSource = tab;
end;