DevExpress中GridControl的使用笔记(转)
转自:https://www.jianshu.com/p/badc1d2f0841
注:练习例子为: DxApplication1 -> XtraForm1 , 例子上传到本博中 2019.4.22
Content
- [Level 1:基本](#Level 1:基本)
- [Level 2:列名](#Level 2:列名)
- [Level 3:格式化显示](#Level 3:格式化显示)
- [Level 4:自定义显示](#Level 4:自定义显示)
- [Level 5:分组](#Level 5:分组)
- [Level 6:汇总](#Level 6:汇总)
- [Level 7:显示行号](#Level 7:显示行号)
- [Level 8:点击事件](#Level 8:点击事件)
- [Level 9:自定义绘制](#Level 9:自定义绘制)
- [Level 10:Cell中的控](#Level 10:Cell中的控)
- [Level 11:BandedGridView](#Level 11:BandedGridView)
- [Level 12:Cell中的控件2](#Level 12:Cell中的控件2)
- [Level 13:主从表(分层表)](#Level 13:主从表(分层表))
Level 1:基本
代码:
private void Form1_Load(object sender, EventArgs e)
{
BindDataSource(InitDt());
}
private DataTable InitDt()
{
DataTable dt = new DataTable("个人简历");
dt.Columns.Add("id", typeof(int));
dt.Columns.Add("name", typeof(string));
dt.Columns.Add("sex", typeof(int));
dt.Columns.Add("address", typeof(string));
dt.Columns.Add("aihao", typeof(string));
dt.Columns.Add("phone", typeof(string));
dt.Rows.Add(new object[] { 1, "张三", 1, "东大街6号", "看书", "12345678910"});
dt.Rows.Add(new object[] { 1, "王五", 0, "西大街2号", "上网,游戏", "" });
dt.Rows.Add(new object[] { 1, "李四", 1, "南大街3号", "上网,逛街", "" });
dt.Rows.Add(new object[] { 1, "钱八", 0, "北大街5号", "上网,逛街,看书,游戏", "" });
dt.Rows.Add(new object[] { 1, "赵九", 1, "中大街1号", "看书,逛街,游戏", ""});
return dt;
}
private void BindDataSource(DataTable dt)
{
//绑定DataTable
gridControl1.DataSource = dt;
}
效果:
![](https://upload-images.jianshu.io/upload_images/816012-4c4f1ff7bd1d63e5.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/699)
Level 2:列名
设置:
![](https://upload-images.jianshu.io/upload_images/816012-e1dc1da31c3edbdb.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1000)
效果:
![](https://upload-images.jianshu.io/upload_images/816012-2b1089e2b0dfa5ca.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/624)
Level 3:格式化显示
代码:
private DataTable InitDt()
{
DataTable dt = new DataTable("个人简历");
dt.Columns.Add("id", typeof(int));
dt.Columns.Add("name", typeof(string));
dt.Columns.Add("sex", typeof(int));
dt.Columns.Add("address", typeof(string));
dt.Columns.Add("aihao", typeof(string));
dt.Columns.Add("phone", typeof(string));
dt.Columns.Add("data", typeof(decimal));
dt.Columns.Add("time", typeof(DateTime));
dt.Columns.Add("custom", typeof(string));
dt.Rows.Add(new object[] { 1, "张三", 1, "东大街6号", "看书", "12345678910",12,"2018/4/26","data"});
dt.Rows.Add(new object[] { 1, "王五", 0, "西大街2号", "上网,游戏", "12315", 23333, "2018/4/26", "test" });
dt.Rows.Add(new object[] { 1, "李四", 1, "南大街3号", "上网,逛街", "", 12.345, "2018/4/26", "" });
dt.Rows.Add(new object[] { 1, "钱八", 0, "北大街5号", "上网,逛街,看书,游戏", "", 0.123, "2018/4/26", "" });
dt.Rows.Add(new object[] { 1, "赵九", 1, "中大街1号", "看书,逛街,游戏", "", 3.1415926, "2018/4/26", "" });
return dt;
}
设置:
![](https://upload-images.jianshu.io/upload_images/816012-616ecd8ff4d88ff0.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/415)
![](https://upload-images.jianshu.io/upload_images/816012-6c683d59b7a6ce94.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/424)
![](https://upload-images.jianshu.io/upload_images/816012-9a95ef27ee7255cf.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/422)
效果:
![](https://upload-images.jianshu.io/upload_images/816012-9bbedf18a1931aa7.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/736)
Tips:
1、gridControl的每一列原始数据是Value,但是显示数据是 DisplayText,默认DisplayText的值即是Value通过DisplayFormat转换之后的值。
2、 gridControl下的事件一般是包含表格GridView切换,点击,更改的事件,用的不多;每一个GridView下的事件包含行列处理,菜单显示,分组排序等事件,我们常用。(所有在使用事件时,一定要明确是control事件还是view事件)
Level 4:自定义显示
代码:
private void gridView1_CustomColumnDisplayText(object sender,
DevExpress.XtraGrid.Views.Base.CustomColumnDisplayTextEventArgs e)
{
if (e.Column.FieldName == "sex")
{
switch (e.Value.ToString().Trim())
{
case "1":
e.DisplayText = "男";
break;
case "0":
e.DisplayText = "女";
break;
default:
e.DisplayText = "";
break;
}}
}
![](https://upload-images.jianshu.io/upload_images/816012-8e666db26922c151.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/389)
效果:
![](https://upload-images.jianshu.io/upload_images/816012-fb3d119b219de5f8.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1000)
Level 5:分组
按照性别进行分组:
![](https://upload-images.jianshu.io/upload_images/816012-c23e15921650955c.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/672)
效果:
![](https://upload-images.jianshu.io/upload_images/816012-441f713db1bcd8ad.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1000)
Level 6:汇总
设置:
1、显示面板
![](https://upload-images.jianshu.io/upload_images/816012-fcd3943730000eac.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/744)
2、汇总项设置
![](https://upload-images.jianshu.io/upload_images/816012-310086b959c10a47.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/791)
效果:
![](https://upload-images.jianshu.io/upload_images/816012-e17120304400995e.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1000)
Level 7:显示行号
设置:
![](https://upload-images.jianshu.io/upload_images/816012-d8a6a45d52f8178e.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/494)
代码:
![](https://upload-images.jianshu.io/upload_images/816012-5bf091fa71dff0a7.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/749)
效果:
![](https://upload-images.jianshu.io/upload_images/816012-86223144867a3473.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1000)
Level 8:点击事件
![](https://upload-images.jianshu.io/upload_images/816012-f5afadf1c4d19b90.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/496)
private void gridView1_RowCellClick(object sender,
DevExpress.XtraGrid.Views.Grid.RowCellClickEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
Console.WriteLine("Button:MouseButtons.Left");
}
if (e.Clicks == 2)
{
Console.WriteLine("Clicks:2");
}
if (e.Delta > 0)
{
Console.WriteLine("Delta > 0");
}
if (e.X > 0 && e.Y >0)
{
Console.WriteLine("Pos:({0},{1})",e.X,e.Y);
}
if (e.RowHandle > 0)
{
Console.WriteLine("Row:{0}",e.RowHandle);
}
if (e.CellValue != null)
{
Console.WriteLine("CellValue:{0}",e.CellValue);
}
if (e.Column != null)
{
Console.WriteLine("Column:{0}",e.Column.ToString());
}
}
Level 9:自定义绘制
代码:
![](https://upload-images.jianshu.io/upload_images/816012-e3d7436cc5d18b6a.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/473)
private void gridView1_CustomDrawCell(object sender,
DevExpress.XtraGrid.Views.Base.RowCellCustomDrawEventArgs e)
{
if (e.Column.FieldName.Equals("data"))
{
GridCellInfo cellInfo = e.Cell as GridCellInfo;
if (cellInfo.IsDataCell )
{
if (double.Parse(cellInfo.CellValue.ToString()) < 1)
{
e.Appearance.BackColor = Color.OrangeRed;
}
else if (double.Parse(cellInfo.CellValue.ToString()) < 100)
{
e.Appearance.BackColor = Color.YellowGreen;
}
else
{
e.Appearance.BackColor = Color.Gold;}
}
}
}
效果:
![](https://upload-images.jianshu.io/upload_images/816012-fdb1ebe4fdd12432.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/857)
Level 10:Cell中的控件
设置:
![](https://upload-images.jianshu.io/upload_images/816012-a09abc17c72da0a3.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/621)
点开ColumnEdit选项,设置选中时数据的类型和值,还可设置灰色和未选中状态的数据。
![](https://upload-images.jianshu.io/upload_images/816012-a7586a90d6fe3717.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/599)
效果:
![](https://upload-images.jianshu.io/upload_images/816012-fd76134921e44458.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/850)
Level 11:BandedGridView
转换为BandedGridView:
![](https://upload-images.jianshu.io/upload_images/816012-881e9259966931a2.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/651)
设置Bands:
![](https://upload-images.jianshu.io/upload_images/816012-be8026db44e47e81.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/919)
效果:
![](https://upload-images.jianshu.io/upload_images/816012-40cda1dd9b0fe52f.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/907)
Level 12:Cell中的控件2
A1、添加按钮控件
1、新增一个无绑定的列
![](https://upload-images.jianshu.io/upload_images/816012-a8756deffb695466.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/207)
2、ColumnEdit选择repositoryItemButtonEdit
![](https://upload-images.jianshu.io/upload_images/816012-e742b4dc534c4ec5.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/496)
3、选择Button项
![](https://upload-images.jianshu.io/upload_images/816012-e7409cbd8f8cee05.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/460)
4、添加新项,修改Caption,并选择Kind为Glyph
![](https://upload-images.jianshu.io/upload_images/816012-7f2d91a311fe43a5.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/507)
5、修改TestEditStyle为HideTextEditor
![](https://upload-images.jianshu.io/upload_images/816012-a51f9bef0bcb1d2e.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/445)
6、选择In-place Editor Repository
,找到新添加的按钮,选择ButtonClick
事件
![](https://upload-images.jianshu.io/upload_images/816012-c0b1eb7a646a3207.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/894)
7、代码
private void repositoryItemButtonEdit1_ButtonClick(object sender,
DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
{
Console.WriteLine("Row:{0},Index:{1}", gridView1.GetFocusedDataSourceRowIndex(), e.Button.Index);
if (e.Button.Index == 0)//删除按钮
{
table.Rows.RemoveAt(gridView1.GetFocusedDataSourceRowIndex());
}
else//编辑按钮
{
MessageBox.Show("编辑 Index:" + e.Button.Index);
}
}
效果:
![](https://upload-images.jianshu.io/upload_images/816012-7c3d6c051ff624d8.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/831)
A2、另一种按钮控件
和上一种方式一样,主要区别为:
* 选择一个绑定了数据的列
- 只添加一个按钮
- 并选择TestEditStyle为
DisableTextEditor
35.png
效果:
![](https://upload-images.jianshu.io/upload_images/816012-8f96cbacbcdb331c.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/834)
A3、各种弹出选项框:
Level 13:主从表(分层表)
待续......
小礼物走一走,来简书
DevExpress中GridControl的使用笔记(转)的更多相关文章
- DevExpress中GridControl的使用笔记
主要参考链接:DevExpress GridControl控件使用 Content [Level 1:基本](#Level 1:基本) [Level 2:列名](#Level 2:列名) [Level ...
- DevExpress中GridControl列转义的实现方法
/// <summary> /// CustomColumnDisplayText Helper /// </summary> /// <param name=" ...
- devexpress中gridcontrol头部添加垂直线(右边框)
winform开发,用devexpress中的gridcontrol控件,头部默认是3D样式,当客户希望像内容一样扁平化显示且需要添加垂直线(右边框)时恶梦开始了..经过一阵摸索发现可以这样解决: 1 ...
- DevExpress中GridControl的属性设置
1.隐藏最上面的GroupPanel gridView1.OptionsView.ShowGroupPanel=false; 2.得到当前选定记录某字段的值 sValue=Table.Rows[gri ...
- devexpress中gridcontrol 一些样式改变
改变footer为扁平化效果 整个footer背景色CustomDrawFootere.Appearance.BackColor = Color.Transparent; e.Appearance.D ...
- DevExpress中GridControl自定义汇总列值(有选择性的汇总)
今天碰到有同事遇到这个方面的需求,贴一下吧. private void gvTop_CustomSummaryCalculate(object sender, CustomSummaryEventAr ...
- DevExpress中GridControl的重新绑定数据后如何刷新 (转)
如果对girdcontrol的datasource新添加数据,重新刷新, gridControl1.DataSource = list; gridView1.RefreshData();
- Devexpress中Gridcontrol查找分组
private void button1_Click(object sender, EventArgs e) { DataTable dt = new DataTable(); dt.Columns. ...
- DevExpress 中 GridControl 的数据源DataTable 内容改变后 重新绑定
1.datatable dt=new datatable(); 2.dt 内容改变 dt.columns.add("col1"); dt.columns.add("col ...
随机推荐
- java关于redis的快速配置
1.关于Jedis安装配置很简单,我主要写一个,能够快速使用redis的工具类,首先导入依赖, 就一个 jedis 最好选用老一点版本 <!-- https://mvnrepository.co ...
- webpack学习笔记 ——篇2
插件整理 extract-text-webpack-plugin 用于将css/less/sass等文件单独打包 https://webpack.docschina.org/plugins/extra ...
- 丰富的else语句以及简介的with语句
1.if···else略 2.while.for与else Python中的循环语句(无论是while 还是for)中,经常配合continue和break语句,此时,可以和else搭配使用,当循环正 ...
- js实现全选反选(开关门)
话不多说直接看图吧
- [转载]Fiddler 解析!抓包抓得好真的可以为所欲为 [一]
说起抓包,很多人以为就是用个工具,简简单单地抓一下就可以了.昨天在面试一个安卓逆向,直接告诉我[抓包没有技术含量].在这里,我必须发一个教程,解析一下抓包神器——Fiddler.Fiddler仅仅是一 ...
- 运动控制之一_PID控制理论
PID算法是早期发展起来的控制算法,该算法因其简单.鲁棒性强且可靠性高而被广泛地应用于过程控制和运动控制中. 常规的PID控制系统原理框图如下所示: PID控制系统原理图 误差信号Err(t)输入到控 ...
- python爬虫套件在mac上的安装-bs的安装
1,首先安装pip gem install pip 这种方式会报错: ERROR: While executing gem ... (Gem::FilePermissionError) You do ...
- javascript 对象数组排序(按照科目级次)
需求 从后台获取的数据是这样的 上帝要这样的 背景 从后台获取到表格数据,然后填充到excel.当然是用js来填充的.js 本身的数组具有sort()功能.但是是针对 ...
- git教程:添加远程仓库
转自: 添加远程仓库 现在的情景是,你已经在本地创建了一个Git仓库后,又想在GitHub创建一个Git仓库,并且让这两个仓库进行远程同步,这样,GitHub上的仓库既可以作为备份,又可以让其他人通过 ...
- asp。net内置委托
Action与Func是APS.NET内置委托 //--------------无返回值的委托Action--------------------------- Action是无返回值的泛型委托 Ac ...