转自: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;
}

效果:

 
1.png

Level 2:列名

设置:

 
2.png

效果:

 
3.png

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

设置:

 
5.png
 
6.png
 
7.png

效果:

 
4.png

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;
}}
}
 
8.png

效果:

 
9.png

Level 5:分组

按照性别进行分组:

 
10.png

效果:

 
11.png

Level 6:汇总

设置:

1、显示面板

 
12.png

2、汇总项设置

 
13.png

效果:

 
14.png

Level 7:显示行号

设置:

 
15.png

代码:

 
16.png

效果:

 
17.png

Level 8:点击事件

 
18.png
 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:自定义绘制

代码:

 
19.png
     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;}
}
}
}

效果:

 
20.png

Level 10:Cell中的控件

设置:

 
21.png

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

 
23.png

效果:

 
22.png

Level 11:BandedGridView

转换为BandedGridView:

 
25.png

设置Bands:

 
26.png

效果:

 
24.png

Level 12:Cell中的控件2

A1、添加按钮控件

1、新增一个无绑定的列

 
27.png

2、ColumnEdit选择repositoryItemButtonEdit

 
28.png

3、选择Button项

 
29.png

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

 
30.png

5、修改TestEditStyle为HideTextEditor

 
31.png

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

 
32.png

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

效果:

 
33.png

A2、另一种按钮控件

和上一种方式一样,主要区别为:

* 选择一个绑定了数据的列
  • 只添加一个按钮
  • 并选择TestEditStyle为DisableTextEditor
     
    35.png

效果:

 
34.png

A3、各种弹出选项框:

参考:DEVEXPRESS GRIDVIEW 代码添加按钮

Level 13:主从表(分层表)

待续......

小礼物走一走,来简书

DevExpress中GridControl的使用笔记(转)的更多相关文章

  1. DevExpress中GridControl的使用笔记

    主要参考链接:DevExpress GridControl控件使用 Content [Level 1:基本](#Level 1:基本) [Level 2:列名](#Level 2:列名) [Level ...

  2. DevExpress中GridControl列转义的实现方法

    /// <summary> /// CustomColumnDisplayText Helper /// </summary> /// <param name=" ...

  3. devexpress中gridcontrol头部添加垂直线(右边框)

    winform开发,用devexpress中的gridcontrol控件,头部默认是3D样式,当客户希望像内容一样扁平化显示且需要添加垂直线(右边框)时恶梦开始了..经过一阵摸索发现可以这样解决: 1 ...

  4. DevExpress中GridControl的属性设置

    1.隐藏最上面的GroupPanel gridView1.OptionsView.ShowGroupPanel=false; 2.得到当前选定记录某字段的值 sValue=Table.Rows[gri ...

  5. devexpress中gridcontrol 一些样式改变

    改变footer为扁平化效果 整个footer背景色CustomDrawFootere.Appearance.BackColor = Color.Transparent; e.Appearance.D ...

  6. DevExpress中GridControl自定义汇总列值(有选择性的汇总)

    今天碰到有同事遇到这个方面的需求,贴一下吧. private void gvTop_CustomSummaryCalculate(object sender, CustomSummaryEventAr ...

  7. DevExpress中GridControl的重新绑定数据后如何刷新 (转)

    如果对girdcontrol的datasource新添加数据,重新刷新, gridControl1.DataSource = list; gridView1.RefreshData();

  8. Devexpress中Gridcontrol查找分组

    private void button1_Click(object sender, EventArgs e) { DataTable dt = new DataTable(); dt.Columns. ...

  9. DevExpress 中 GridControl 的数据源DataTable 内容改变后 重新绑定

    1.datatable dt=new datatable(); 2.dt 内容改变 dt.columns.add("col1"); dt.columns.add("col ...

随机推荐

  1. Excel 使用单元格的值 查询MySQL数据库并返回数据给相应的单元格

    Dim MyConn As ObjectPrivate Sub ConnectDB()Set MyConn = CreateObject("ADODB.Connection")   ...

  2. hdu4044

    题意:给你一颗树有n个节点,树的根节点为1,表示为敌人的基地,其他叶子节点为你的基地,你一开始有m元,给你每个节点可以建造的塔的数量和塔的价格和可以照成的伤害,每个节点至多建立一座塔.敌人的基地每次会 ...

  3. 共有和私有、name mangling

    1.在python中可以在变量和函数名前加上双下划线—‘’__‘’来实现其伪私有(实际上python中没有Private属性的),加上双下划线后,外部对象不能通过调用其名称直接获得对象的属性或操作. ...

  4. Big Txt File(一)

    对于当今的数据集来说,动不动就上G的大小,市面的软件大多不支持,所以需要自己写一个. 常见的txt文本行形式存储的时候也不过是行数多些而已,可以考虑只观测部分行的方式,基于这个思路可以搞一个大数据的浏 ...

  5. 在过去五分钟内,TypeScript语言服务以外终止了5次

    这个问题困扰了我两次...第一次重装了VS CODE 具体的原意找到之后我直接想骂娘....... 各位如果碰到这个问题....请打开你的360安全卫士!!! 注意看看您家360的防护日志有木有贴心帮 ...

  6. 【4】数独(Sudoku Killer)(深度优先遍历)

    问题描述:给你多个数独题目,让你输出答案 思路:递归确定每一个‘?’的位置的值,直到所有‘?’都被确定.先将原字符数组转换为整型数组,‘?’由数字0代替,然后每一次层递归找到第一个0的位置,如果找到了 ...

  7. nginx 带? rewrite 规则

    由于需要重定向 url ,nginx需要rewrite .参考文献 http://huangqiqing123.iteye.com/blog/2083434 需求:将http://10.106.1.3 ...

  8. python常量 变量 数据类型 注释 交互 if语句

    1.认识python python的创始人吉多·范罗苏姆(Guido van Rossum )龟叔在89年圣诞节开始开发python python属于解释型语言,强类型定义语言. 编译型/解释型:编译 ...

  9. ssm配置文件叙述

    spring+springmvc+mybatis框架中用到了三个XML配置文件:web.xml,spring-mvc.xml,spring-mybatis.xml.第一个不用说,每个web项目都会有的 ...

  10. [转载]JS中 map, filter, some, every, forEach, for in, for of 用法总结

    转载地址:http://codebay.cn/post/2110.html 1.map 有返回值,返回一个新的数组,每个元素为调用func的结果. let list = [1, 2, 3, 4, 5] ...