DevExpress的GridControl的使用以及怎样添加列和绑定数据源
程序员文章站
2023-12-29 00:00:22
场景 Winform控件-DevExpress18下载安装注册以及在VS中使用: https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/100061243 在上面搭建好DevExpress的环境后,要使用其GridControl控件。 注 ......
场景
winform控件-devexpress18下载安装注册以及在vs中使用:
https://blog.csdn.net/badao_liumang_qizhi/article/details/100061243
在上面搭建好devexpress的环境后,要使用其gridcontrol控件。
注:
博客主页:
关注公众号
霸道的程序猿
获取编程相关电子书、教程推送与免费下载
实现
首先在窗体中拖拽一个gridcontrol
然后在窗体的load时事件中对其进行添加列和样式设置
private void frmsearch_load(object sender, eventargs e) { //设置gridcontrol样式 common.gridcontrol.gridcontrolhelper.setstyles(this.gridcontrol1.mainview as devexpress.xtragrid.views.base.columnview); //订阅行点击事件 this.gridview1.rowclick += gridview1_rowclick; }
进入设置样式的方法
public static void setstyles(devexpress.xtragrid.views.base.columnview view) { if (view is devexpress.xtragrid.views.grid.gridview) { devexpress.xtragrid.views.grid.gridview gridview = view as devexpress.xtragrid.views.grid.gridview; gridview.optionsview.showgrouppanel = false; //隐藏最上面的grouppanel gridview.optionsview.showindicator = false; //隐藏指示列 gridview.focusrectstyle = devexpress.xtragrid.views.grid.drawfocusrectstyle.none; //设置焦点框为整行 gridview.optionsselection.enableappearancefocusedcell = false; //禁用单元格焦点 gridview.optionsselection.enableappearancefocusedrow = true; //启用整行焦点 gridview.optionsselection.enableappearancefocusedrow = true; //启用整行焦点 gridview.optionsselection.enableappearancehideselection = false; gridview.optionsview.enableappearanceevenrow = true; //启用偶数行背景色 gridview.optionsview.enableappearanceoddrow = true; //启用奇数行背景色 //gridview.appearance.evenrow.backcolor = system.drawing.color.fromargb(150, 237, 243, 254); //设置偶数行背景色 //gridview.appearance.oddrow.backcolor = system.drawing.color.fromargb(150, 199, 237, 204); //设置奇数行背景色 //gridview.appearance.focusedrow.backcolor = system.drawing.color.red; //gridview.appearance.selectedrow.backcolor = system.drawing.color.red; } //禁用自动生成列 view.optionsbehavior.autopopulatecolumns = false; //禁用自动列宽 if (view is devexpress.xtragrid.views.grid.gridview) { (view as devexpress.xtragrid.views.grid.gridview).optionsview.columnautowidth = false; } //禁用数据过滤面板 view.optionsview.showfilterpanelmode = devexpress.xtragrid.views.base.showfilterpanelmode.never; #region 添加列 view.columns.clear(); int index = 0; devexpress.xtragrid.columns.gridcolumn col = null; col = new devexpress.xtragrid.columns.gridcolumn(); col.fieldname = "dbname"; col.caption = "数据库名"; col.width = 200; col.visibleindex = index++; view.columns.add(col); col = new devexpress.xtragrid.columns.gridcolumn(); col.fieldname = "shortnodetext"; col.caption = "文件名"; col.width = 200; col.visibleindex = index++; view.columns.add(col); col = new devexpress.xtragrid.columns.gridcolumn(); col.fieldname = "createdate"; col.caption = "创建日期"; col.width = 130; col.visibleindex = index++; view.columns.add(col); col = new devexpress.xtragrid.columns.gridcolumn(); col.fieldname = "taskfile"; col.caption = "任务文件"; col.width = 180; col.visibleindex = index++; view.columns.add(col); col = new devexpress.xtragrid.columns.gridcolumn(); col.fieldname = "fullpath"; col.caption = "完整路径"; col.width = 180; col.visibleindex = index++; view.columns.add(col); col = new devexpress.xtragrid.columns.gridcolumn(); col.fieldname = "barcode"; col.caption = "电池条码"; col.width = 180; col.visibleindex = index++; view.columns.add(col); #endregion setallowedit(view, false); //禁用编辑 setallowsort(view, devexpress.utils.defaultboolean.false); //禁用排序 setallowfilter(view, false); //禁用数据过滤 }
在上面方法中进行样式的设置以及列的添加
注意在添加列时fieldname 属性要与将来设置数据源时的字段一致。
然后上面的禁用编辑的方法
public static void setallowedit(devexpress.xtragrid.views.base.columnview view, bool isallow) { foreach (devexpress.xtragrid.columns.gridcolumn col in view.columns) { col.optionscolumn.allowedit = isallow; } }
禁用排序的方法
public static void setallowsort(devexpress.xtragrid.views.base.columnview view, devexpress.utils.defaultboolean value) { foreach (devexpress.xtragrid.columns.gridcolumn col in view.columns) { col.optionscolumn.allowsort = value; } }
禁用数据过滤的方法
public static void setallowfilter(devexpress.xtragrid.views.base.columnview view, bool isallow) { foreach (devexpress.xtragrid.columns.gridcolumn col in view.columns) { col.optionsfilter.allowautofilter = isallow; col.optionsfilter.allowfilter = isallow; } }
初始化完样式和添加列后就要设置数据源
首先新建一个实体对象,对象要有与上面添加列时fieldname 所对应的属性。
下面是部门字段和属性,其他省略
public class datatreenode { private string id; private string parentid; private string nodetext; private string createdate; private string fullpath; private string taskfile; private string barcode; private datatreenodetypes nodetype = datatreenodetypes.folder; /// <summary> /// 去掉扩展名的数据文件完整路径 /// </summary> public string id { get { return id; } set { id = value; } } /// <summary> /// 父级节点的id /// </summary> public string parentid { get { return parentid; } set { parentid = value; } } /// <summary> /// 数据文件名称 /// </summary> public string nodetext { get { return nodetext; } set { nodetext = value; } } }
构建数据源
list<datatreenode> data = new list<datatreenode>(); data = datatreelisthelper.parsedir(common.global.appconfig.testdatadir, data); var result = data.where(p => p.nodetype = = datatreenodetypes.file);
首先声明上面实体对象的list,然后使用parsedir方法将文件目录进行递归查询。
然后进行筛选出文件类型。
然后可以直接设置数据源
this.gridcontrol1.datasource = result;