Devexpressdatagrid动态添加显⽰指定列的gridView
代码如下:
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 = 0; 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[0].Kind = DevExpress.XtraEditors.Controls.ButtonPredefines.Glyph;
rib.ButtonClick += clickFunction;
rib.Buttons[0].Caption = btnText;
if(skinName!="")
rib.LookAndFeel.SkinName = skinName;
rib.LookAndFeel.UseDefaultLookAndFeel = false;
if (image != null)
{
rib.Buttons[0].Image = image;
rib.Buttons[0].ImageLocation = location;
}
rib.Buttons[0].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
param name{
///<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 = -1;
}
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=0,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;

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。