DevExpress表格控件XtraGrid实现动态列表查询

DevExpress表格控件XtraGrid实现动态列表查询CRM应用系统有很多的列表查询,用于展示列表数据。DevExpress的表格控件XtraGrid可以动态创建列展示列表数据。一、创建基础自定义控

大家好,欢迎来到IT知识分享网。

CRM应用系统有很多的列表查询,用于展示列表数据。DevExpress的表格控件XtraGrid可以动态创建列展示列表数据。

一、创建基础自定义控件ListUserControl

这个控件用于列表数据的展示及相关操作,设计界面如下图:

DevExpress表格控件XtraGrid实现动态列表查询

1.1设置gridview1的属性如下图,不可编辑,隐藏分组面板

DevExpress表格控件XtraGrid实现动态列表查询

1.2定义对象名属性

private string _objName;

/// <summary>

/// 对象名

/// </summary>

public string ObjName

{

get { return _objName; }

set { _objName = value; }

}

1.3编写获取列表布局字段方法ListUserListField

/// <summary>

/// 获取列表布局字段,已选择字段,已过滤字段

/// </summary>

/// <param name=”ObjName”>对象名称</param>

/// <returns></returns>

private UserListProfileOut ListUserListField(string ObjName)

{

var obj = SeageModel.model.DDObjectService.GetObjectByObjName(ObjName);

if (obj == null)

{

return null;

}

var userlistprofile = SeageModel.model.UserListProfileService.GetUserListProfile(PublicVar.CurrentUser.UserID, obj.ObjType);

//已选字段

var fields = userlistprofile.QuickListFlds.Split(‘;’);

var flist = SeageModel.model.DictStrService.ListAllDictStrByObjName(PublicVar.DefaultLang.LangId, ObjName);

flist = flist.Where(e => fields.Contains(e.DictName)).ToList();

//过滤字段

string[] filterfields = “”.Split(‘;’);

if (userlistprofile.QuickCondFlds != null)

{

filterfields = userlistprofile.QuickCondFlds.Split(‘;’);

}

var ffilterlist = SeageModel.model.DictStrService.ListAllDictStrByObjName(PublicVar.DefaultLang.LangId, ObjName);

ffilterlist = ffilterlist.Where(e => filterfields.Contains(e.DictName)).ToList();

//再排序

List<DictStr> fselectedlist = new List<DictStr>();

foreach (var item in fields)

{

foreach (var itemdo in flist)

{

if (itemdo.DictName == item)

{

fselectedlist.Add(itemdo);

}

}

}

UserListProfileOut userlistout = new UserListProfileOut();

userlistout.ListFlds = fselectedlist;

foreach (var item in userlistout.ListFlds)

{

var items = SeageModel.model.EnumItemStrService.GetEnumStrItem(PublicVar.DefaultLang.LangId, item.DictName);

item.EnumStrItems = items;

}

userlistout.FilterListFlds = ffilterlist;

//可选字段

var fslist = SeageModel.model.DictStrService.ListAllDisplayDictStrByObjName(PublicVar.DefaultLang.LangId, ObjName)

.Where(e => !fields.Contains(e.DictName)).ToList();

userlistout.SelectFlds = fslist;

var ffslist = SeageModel.model.DictStrService.ListAllDisplayDictStrByObjName(PublicVar.DefaultLang.LangId, ObjName)

.Where(e => !filterfields.Contains(e.DictName)).ToList();

userlistout.FilterFlds = ffslist;

return userlistout;

}

1.4初始化表格控件InitGrid

public void InitGrid()

{

UserListProfileOut userListProfileOut = ListUserListField(_objName);

if(userListProfileOut!=null)

{

gridView1.Columns.Clear();

GridColumn gridColumn;

//初始化表格控件

foreach (var item in userListProfileOut.ListFlds)

{

gridColumn = gridView1.Columns.Add();

gridColumn.FieldName = item.DDAttr.AttrName.Split(‘.’)[1];

gridColumn.Caption = item.Label;

gridColumn.Visible = true;

//字段类型

switch ((FieldType)item.DDAttr.AttrType)

{

case FieldType.Number:

case FieldType.Currency:

gridColumn.SummaryItem.FieldName= item.DDAttr.AttrName;

gridColumn.SummaryItem.SummaryType = DevExpress.Data.SummaryItemType.Sum;

break;

case FieldType.Text:

case FieldType.TextArea:

case FieldType.TextAreaLarge:

break;

case FieldType.DateType:

break;

case FieldType.DateTimeType:

break;

case FieldType.CheckBox:

RepositoryItemCheckEdit repositoryItemCheckEdit = new RepositoryItemCheckEdit();

//repositoryItemCheckEdit.DisplayValueChecked = “1”;

repositoryItemCheckEdit.ValueChecked = “1”;

repositoryItemCheckEdit.ValueUnchecked = “0”;

gridColumn.ColumnEdit = repositoryItemCheckEdit;

break;

case FieldType.SingleDropDown:

RepositoryItemLookUpEdit combo = new RepositoryItemLookUpEdit();

combo.DataSource = item.EnumStrItems;

combo.KeyMember = “EnumKey”;

combo.ValueMember = “EnumKey”;

combo.DisplayMember = “EnumValue”;

gridColumn.ColumnEdit = combo;

break;

case FieldType.WebUrl:

case FieldType.Url:

RepositoryItemHyperLinkEdit hyperLinkEdit = new RepositoryItemHyperLinkEdit();

gridColumn.ColumnEdit = hyperLinkEdit;

break;

case FieldType.ImageType:

RepositoryItemImageEdit itemImageEdit = new RepositoryItemImageEdit();

gridColumn.ColumnEdit = itemImageEdit;

break;

case FieldType.RefType:

RepositoryItemLookUpEdit repositoryItemLookUpEdit = new RepositoryItemLookUpEdit();

string fieldname = item.DictName.Split(‘.’)[1];

if (fieldname == “DepartmentID”)

{

repositoryItemLookUpEdit.DataSource = SeageModel.model.DepartmentService.ListOrg();

repositoryItemLookUpEdit.ValueMember = “DeptID”;

repositoryItemLookUpEdit.KeyMember = “DeptID”;

repositoryItemLookUpEdit.DisplayMember = “Name”;

LookUpColumnInfo lookUpColumnInfo = new LookUpColumnInfo();

lookUpColumnInfo.FieldName = “DeptID”;

lookUpColumnInfo.Caption = “ID”;

repositoryItemLookUpEdit.Columns.Add(lookUpColumnInfo);

LookUpColumnInfo lookUpColumnInfo1 = new LookUpColumnInfo();

lookUpColumnInfo1.Caption = “名称”;

lookUpColumnInfo1.FieldName = “Name”;

repositoryItemLookUpEdit.Columns.Add(lookUpColumnInfo1);

gridColumn.ColumnEdit = repositoryItemLookUpEdit;

}

if ((fieldname == “CreatedByID”) || (fieldname == “ModifiedByID”) || (fieldname == “DeletedByID”)||(fieldname==”OwnerID”))

{

repositoryItemLookUpEdit.DataSource = SeageModel.model.UserService.ListUsers();

repositoryItemLookUpEdit.ValueMember = “UserID”;

repositoryItemLookUpEdit.KeyMember = “UserID”;

repositoryItemLookUpEdit.DisplayMember = “Name”;

LookUpColumnInfo lookUpColumnInfo = new LookUpColumnInfo();

lookUpColumnInfo.Caption = “ID”;

lookUpColumnInfo.FieldName = “UserID”;

repositoryItemLookUpEdit.Columns.Add(lookUpColumnInfo);

LookUpColumnInfo lookUpColumnInfo1 = new LookUpColumnInfo();

lookUpColumnInfo1.Caption = “姓名”;

lookUpColumnInfo1.FieldName = “Name”;

repositoryItemLookUpEdit.Columns.Add(lookUpColumnInfo1);

gridColumn.ColumnEdit = repositoryItemLookUpEdit;

}

break;

default:

break;

}

}

foreach (var item in userListProfileOut.SelectFlds)

{

gridColumn = gridView1.Columns.Add();

gridColumn.FieldName = item.DDAttr.AttrName.Split(‘.’)[1];

gridColumn.Caption = item.Label;

gridColumn.Visible = false;

//字段类型

switch ((FieldType)item.DDAttr.AttrType)

{

case FieldType.Number:

case FieldType.Currency:

gridColumn.SummaryItem.FieldName = item.DDAttr.AttrName;

gridColumn.SummaryItem.SummaryType = DevExpress.Data.SummaryItemType.Sum;

break;

case FieldType.Text:

case FieldType.TextArea:

case FieldType.TextAreaLarge:

break;

case FieldType.DateType:

break;

case FieldType.DateTimeType:

break;

case FieldType.CheckBox:

RepositoryItemCheckEdit repositoryItemCheckEdit = new RepositoryItemCheckEdit();

// repositoryItemCheckEdit.DisplayValueChecked = “1”;

gridColumn.ColumnEdit = repositoryItemCheckEdit;

break;

case FieldType.SingleDropDown:

RepositoryItemLookUpEdit combo = new RepositoryItemLookUpEdit();

combo.DataSource = item.EnumStrItems;

combo.KeyMember = “EnumKey”;

combo.ValueMember = “EnumKey”;

combo.DisplayMember = “EnumValue”;

gridColumn.ColumnEdit = combo;

break;

case FieldType.WebUrl:

case FieldType.Url:

RepositoryItemHyperLinkEdit hyperLinkEdit = new RepositoryItemHyperLinkEdit();

gridColumn.ColumnEdit = hyperLinkEdit;

break;

case FieldType.ImageType:

RepositoryItemImageEdit itemImageEdit = new RepositoryItemImageEdit();

gridColumn.ColumnEdit = itemImageEdit;

break;

case FieldType.RefType:

RepositoryItemLookUpEdit repositoryItemLookUpEdit = new RepositoryItemLookUpEdit();

string fieldname = item.DictName.Split(‘.’)[1];

if (fieldname == “DepartmentID”)

{

repositoryItemLookUpEdit.DataSource = SeageModel.model.DepartmentService.ListOrg();

repositoryItemLookUpEdit.ValueMember = “Name”;

repositoryItemLookUpEdit.KeyMember = “DeptID”;

LookUpColumnInfo lookUpColumnInfo = new LookUpColumnInfo();

lookUpColumnInfo.FieldName = “DeptID”;

lookUpColumnInfo.Caption = “ID”;

repositoryItemLookUpEdit.Columns.Add(lookUpColumnInfo);

LookUpColumnInfo lookUpColumnInfo1 = new LookUpColumnInfo();

lookUpColumnInfo1.Caption = “名称”;

lookUpColumnInfo1.FieldName = “Name”;

repositoryItemLookUpEdit.Columns.Add(lookUpColumnInfo1);

gridColumn.ColumnEdit = repositoryItemLookUpEdit;

}

if ((fieldname == “CreatedByID”) || (fieldname == “ModifiedByID”) || (fieldname == “DeletedByID”)||(fieldname==”OwnerID”))

{

repositoryItemLookUpEdit.DataSource = SeageModel.model.UserService.ListUsers();

repositoryItemLookUpEdit.ValueMember = “Name”;

repositoryItemLookUpEdit.KeyMember = “UserID”;

LookUpColumnInfo lookUpColumnInfo = new LookUpColumnInfo();

lookUpColumnInfo.Caption = “ID”;

lookUpColumnInfo.FieldName = “UserID”;

repositoryItemLookUpEdit.Columns.Add(lookUpColumnInfo);

LookUpColumnInfo lookUpColumnInfo1 = new LookUpColumnInfo();

lookUpColumnInfo1.Caption = “姓名”;

lookUpColumnInfo1.FieldName = “Name”;

repositoryItemLookUpEdit.Columns.Add(lookUpColumnInfo1);

gridColumn.ColumnEdit = repositoryItemLookUpEdit;

}

break;

default:

break;

}

}

//初始化查询过滤面板

InitQueryPanel(userListProfileOut);

}

}

二、继承ListUserControl新建AccountUserControl控件

DevExpress表格控件XtraGrid实现动态列表查询

三、点击菜单实例化AccountUserControl控件,最终运行的效果图如下:

DevExpress表格控件XtraGrid实现动态列表查询

小结表格控件XtraGrid有各种编辑显示列控件,利用这些控件与数据库的字段属性定义可以动态的创建显示列,达到想要的效果,这样就不需要每一个对象都去设计列表控件。表格列控件有如下图这些:

DevExpress表格控件XtraGrid实现动态列表查询

可以足够丰富的显示各种数据类型。

免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://yundeesoft.com/48754.html

(0)

相关推荐

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注微信