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;
}
效果:

Level 2:列名
设置:

效果:

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;
}
设置:



效果:

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;
}}
}

效果:

Level 5:分组
按照性别进行分组:

效果:

Level 6:汇总
设置:
1、显示面板

2、汇总项设置

效果:

Level 7:显示行号
设置:

代码:

效果:

Level 8:点击事件

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:自定义绘制
代码:

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;}
}
}
}
效果:

Level 10:Cell中的控件
设置:

点开ColumnEdit选项,设置选中时数据的类型和值,还可设置灰色和未选中状态的数据。

效果:

Level 11:BandedGridView
转换为BandedGridView:

设置Bands:

效果:

Level 12:Cell中的控件2
A1、添加按钮控件
1、新增一个无绑定的列

2、ColumnEdit选择repositoryItemButtonEdit

3、选择Button项

4、添加新项,修改Caption,并选择Kind为Glyph

5、修改TestEditStyle为HideTextEditor

6、选择In-place Editor Repository,找到新添加的按钮,选择ButtonClick事件

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);
}
}
效果:

A2、另一种按钮控件
和上一种方式一样,主要区别为:
* 选择一个绑定了数据的列
- 只添加一个按钮
- 并选择TestEditStyle为
DisableTextEditor
35.png
效果:

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 ...
随机推荐
- Linux集群架构(一)
第二十八课 Linux集群架构(一) 目录 一. 集群介绍 二. keepalived介绍 三. 用keepalived配置高可用集群 四. 负载均衡集群介绍 五. LVS介绍 六. LVS调度算法 ...
- μCOS-Ⅲ——常用注意事项
**1,**main函数在调用其他函数之前必须先调用OSInit()函数对内核进行初始化. 2,所有的错误类型码都以OS_ERR_为前缀, 3,命名时尽量统一个格式,所有的函数.变量.宏定义和#def ...
- jsp视频如何播放
网站开发小白们对如何插入视频有较大的困扰,一段时间不知道从何下手,想在数据库里面直接导入,但没能成功,后又尝试直接在myeclipse里面直接放入视频. 对于不同的播放器,视频的格式也有要求,建议使用 ...
- 【亲测】Asp.net Mvc5 + EF6 code first 方式连接MySQL总结
本文原文地址为:https://www.cnblogs.com/summit7ca/p/5423637.html 原文测试环境为windows 8.1+Vs2013+MySql5.7.12 本人在wi ...
- JS中的一元操作符
表达式 一元操作符 优先级 结合性 运算顺序 表达式是什么? 就是JS 中的一个短语,解释器遇到这个短语以后会把对它进行计算,得到一个结果参与运算,我们把这种要参与到运算中的各种各样的短语称为表达式. ...
- 浅谈requireJS 摘自http://www.cnblogs.com/giggle/p/5436710.html
项目中大都使用模块化开发,requireJS作为AMD模块开发的典范,所以有必要学习下.通过一步步利用requireJS编写demo,从而学习requireJS的一个整体开发流程以及自我使用requi ...
- 【原创】Arduino入门基础知识总结
一.概述 Arduino是源自意大利的一个开放源代码的硬件项目平台,该平台包括一块具备简单I/O功能的电路板以及一套程序开发环境软件. Arduino可以用来开发交互产品,比如它可以读取大量的开 ...
- Java中next()和nextLine()的区别
首先,next()一定要读取到有效字符后才可以结束输入,对输入有效字符之前遇到的空格键.Tab键或Enter键等结束符,next()方法会自动将其去掉,只有在输入有效字符之后,next()方法才将其后 ...
- liunx centOS6.5安装jdk教程
第一步:mkdir /usr/java/ --在/usr目录下新建一个java目录 第二步:cp jdk-7u25-linux-x64.rpm /usr/java/ --将JDK文件复制到/usr/j ...
- ios 视图既显示阴影又有圆角实现
//- (UIView *)createTimeBG //{ // UIView *view = [[UIView alloc]init]; // view.backgroundColor ...