当前位置: 首页 > news >正文

【C#】 GridControl与GridView、容器和视图

GridControl与GridView

  • GridControl(容器)

    • 放在窗体上的那个大控件,负责承载视图、绑定 DataSource、滚动、打印、主从关系等。
    • 例如:gridControlLogs.DataSource = _logs;
  • GridView(视图)

    • 显示数据的表格视图(列、排序、过滤、行样式都是它管)。
    • 放在 gridControlLogs.ViewCollection 里,其中一个被设为 MainView
    • 常用:gridViewLogs.OptionsBehavior.Editable = false;gridViewLogs.RowStyle += ...

正确的命名与结构(应该有两个字段)

Form1.Designer.cs 里最好看到类似:

private DevExpress.XtraGrid.GridControl gridControlLogs;
private DevExpress.XtraGrid.Views.Grid.GridView gridViewLogs;...this.gridControlLogs.MainView = this.gridViewLogs;
this.gridControlLogs.ViewCollection.AddRange(new DevExpress.XtraGrid.Views.Base.BaseView[] {this.gridViewLogs});

如果没有,按下面修:

方式A(最简单)

  1. 直接删掉现在这个 GridControl,重新从工具箱拖一个 GridControl 到“日志”面板。
  2. 设计器会自动配一个 GridView(一般叫 gridView1)。
  3. 把它们重命名为:gridControlLogsgridViewLogs

方式B(不删)

  1. 选中当前控件,把 (Name) 改成 gridControlLogs

  2. 打开 Smart Tag(控件右上角小箭头)→ Run Designer… → 左侧 Views

    • 如果没有 GridView:点 Add 添加 GridView,把 Set as MainView 打上。
    • 把该视图的 Name 改成 gridViewLogs

代码里怎么用(容器绑定、视图美化)

// 数据绑定 -> 绑在 GridControl 上
gridControlLogs.DataSource = _logs;// 视图设置 -> 操作 GridView
var gv = gridViewLogs;
gv.OptionsBehavior.Editable = false;
gv.OptionsView.ShowGroupPanel = false;
gv.OptionsView.ColumnAutoWidth = true;gv.Columns[nameof(LogItem.Time)].Caption = "时间";
gv.Columns[nameof(LogItem.Text)].Caption = "内容";
gv.Columns[nameof(LogItem.Time)].DisplayFormat.FormatType = DevExpress.Utils.FormatType.DateTime;
gv.Columns[nameof(LogItem.Time)].DisplayFormat.FormatString = "HH:mm:ss.fff";// 行着色(根据我们保存的 ARGB)
gv.RowStyle += (s, ev) =>
{var item = gv.GetRow(ev.RowHandle) as LogItem;if (item?.ColorArgb != null)ev.Appearance.ForeColor = Color.FromArgb(item.ColorArgb.Value);
};

小提示:若你不确定有没有视图,也可以在代码里兜底:

using DevExpress.XtraGrid.Views.Grid;
if (gridControlLogs.MainView == null)
{var view = new GridView(gridControlLogs);gridControlLogs.MainView = view;gridControlLogs.ViewCollection.Add(view);gridViewLogs = view;
}

总结:GridControl 绑定数据 & 承载视图GridView 负责表格呈现。两者缺一不可,名字不要混。

http://www.dtcms.com/a/333096.html

相关文章:

  • Spring事务 概念 配置 隔离级别 脏读幻读不可重复读 传播行为
  • pyinstaller-从安装到高级使用
  • align-content 设置侧轴上的子元素的排列方式(多行)
  • Git代码版本管理
  • OpenCV---getStructuringElement 结构元素获取
  • 设计心得——如何架构选型
  • ffmpeg 安装、配置与使用完全指南
  • 自学大语言模型之Transformer的Tokenizer
  • jenkins 自动部署
  • 开发Chrome/Edge插件基本流程
  • mysql中in 和 exists 区别
  • 从传感器到大模型:Jetson Thor + LLM.VLA + Holoscan 的边缘推理全链路实战
  • 基于改进Apriori算法的Web文档聚类方法研究(一)
  • 20250815给ubuntu22.04.5的系统缩小/home分区
  • Doris FE 应急恢复手册:六大经典故障场景与解决方案
  • WITRAN:基于改进的RNN时间序列预测模型
  • rent8 安装部署教程之 Windows
  • Effective C++ 条款43:学习处理模板化基类内的名称
  • 俄罗斯信封套娃问题-二维最长递增子序列
  • 【JavaEE】多线程 -- 线程安全
  • UI-TARS-Desktop 深度解析:下一代智能自动化桌面平台
  • Stagehand深度解析:从开源自动化工具到企业级RPA平台的演进之路
  • 神经网络 小土堆pytorch记录
  • nVidia Tesla P40使用anaconda本地重编译pytorch3d成功加载ComfyUI-3D-Pack
  • 基于多分类的工业异常声检测及应用
  • 微信小程序 拖拽签章
  • C语言基础00——基本补充(#define)
  • useEffect 和 useLayoutEffect 执行时机
  • 【补充】数据库中有关系统编码和校验规则的简述
  • 网络性能排查