DevExpress中GridControl的使用笔记
主要参考链接:DevExpress GridControl控件使用
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、另一种按钮控件
和上一种方式一样,主要区别为:
* 选择一个绑定了数据的列
DisableTextEditor

效果:

A3、各种弹出选项框:
Level 13:主从表(分层表)
待续......
=
关注我】。(●'◡'●)
如果,您希望更容易地发现我的新博客,不妨点击一下绿色通道的【因为,我的写作热情也离不开您的肯定与支持,感谢您的阅读,我是【Jack_孟】!
本文来自博客园,作者:jack_Meng,转载请注明原文链接:https://www.cnblogs.com/mq0036/p/10623012.html
【免责声明】本文来自源于网络,如涉及版权或侵权问题,请及时联系我们,我们将第一时间删除或更改!
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 没有源码,如何修改代码逻辑?
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· .NET Core 托管堆内存泄露/CPU异常的常见思路
· DeepSeek “源神”启动!「GitHub 热点速览」
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· C# 集成 DeepSeek 模型实现 AI 私有化(本地部署与 API 调用教程)
· DeepSeek R1 简明指南:架构、训练、本地部署及硬件要求
· NetPad:一个.NET开源、跨平台的C#编辑器