C# superGridControl 样式设置、加载数据、获取数据
样式设置
superGridControl1.PrimaryGrid.SelectionGranularity = SelectionGranularity.Cell; //设置选中样式 单元格、整列、单元格和整列 superGridControl1.PrimaryGrid.MultiSelect = false; //设置是否可以选中多行 superGridControl1.PrimaryGrid.RowDragBehavior = RowDragBehavior.None; superGridControl1.PrimaryGrid.EnableCellMerging = true; superGridControl1.PrimaryGrid.ShowRowGridIndex = true;//显示行号 superGridControl1.PrimaryGrid.AllowRowHeaderResize = true;//允许调整行头的宽度 superGridControl1.PrimaryGrid.ShowRowHeaders = false; //不允许显示行头 superGridControl1.PrimaryGrid.EnableFiltering = true; superGridControl1.PrimaryGrid.EnableColumnFiltering = true;//让列头显示筛选图标 superGridControl1.PrimaryGrid.RowHeaderIndexOffset = 1;//设置行号的开始值 this.superGridControl1.PrimaryGrid.Filter.Visible = true;//显示Filter this.superGridControl1.PrimaryGrid.GroupByRow.Visible = true; //允许按列分组 // GridPanel panel = superGridControl1.PrimaryGrid; panel.SetGroup(panel.Columns["Period"]);//使用分组
SPG.PrimaryGrid.ShowRowGridIndex = false;//设置行号 SPG.PrimaryGrid.SelectionGranularity = SelectionGranularity.Row; SPG.BackColor = Color.FromArgb(8, 47, 76);//设置控件的整个背景颜色不包含显示内容 SPG.PrimaryGrid.DefaultVisualStyles.GridPanelStyle.Background.Color1 = Color.FromArgb(8, 47, 76); SPG.PrimaryGrid.DefaultRowHeight = 0; SPG.PrimaryGrid.ShowRowHeaders = false; SPG.PrimaryGrid.AllowEdit = false; SPG.DefaultVisualStyles.ColumnHeaderStyles.Default.Background.Color1 = Color.FromArgb(8, 47, 76);//设置背景颜色紧紧加载的内容 SPG.DefaultVisualStyles.ColumnHeaderStyles.Default.Background.Color2 = Color.FromArgb(8, 47, 76);//设置背景颜色紧紧加载的内容
SPG.DefaultVisualStyles.ColumnHeaderStyles.Default.TextColor = Color.White; SPG.DefaultVisualStyles.CellStyles.Default.Alignment = DevComponents.DotNetBar.SuperGrid.Style.Alignment.MiddleCenter;//设置列头文字居中问题
加载数据
未设置列的情况下直接绑定数据源
DataRow DR = DTS.NewRow();
DR["调研代码"] = "1";
DR["调研序列"] = "2";
DR["调研维度"] = "3";
DR["维度说明"] = "4";
DR["核算单元1"] = "5";
DR["核算单元2"] = "6";
DTS.Rows.Add(DR);
superGridControl1.PrimaryGrid.DataSource = DTS;
当然在绑定列的情况下 如下 也可以绑定数据源
GridColumn col = new GridColumn();
col = new GridColumn();
col.Name = "调研代码";
col.HeaderText = "调研代码";
col.EditorType = typeof(GridLabelXEditControl);
col.FilterMatchType = FilterMatchType.RegularExpressions;
col.CellMergeMode = CellMergeMode.None;
col.AutoSizeMode = ColumnAutoSizeMode.None;
col.Visible = false;
superGridControl1.PrimaryGrid.Columns.Add(col);
col = new GridColumn();
col.Name = "调研序列";
col.HeaderText = "调研序列";
col.EditorType = typeof(GridLabelXEditControl);
col.FilterMatchType = FilterMatchType.RegularExpressions;
col.CellMergeMode = CellMergeMode.None;
col.AutoSizeMode = ColumnAutoSizeMode.None;
superGridControl1.PrimaryGrid.Columns.Add(col);
col = new GridColumn();
col.Name = "调研维度";
col.HeaderText = "调研维度";
col.EditorType = typeof(GridLabelXEditControl);
col.FilterMatchType = FilterMatchType.RegularExpressions;
col.CellMergeMode = CellMergeMode.None;
col.AutoSizeMode = ColumnAutoSizeMode.None;
superGridControl1.PrimaryGrid.Columns.Add(col);
col = new GridColumn();
col.Name = "维度说明";
col.HeaderText = "维度说明";
col.EditorType = typeof(GridLabelXEditControl);
col.FilterMatchType = FilterMatchType.RegularExpressions;
col.CellMergeMode = CellMergeMode.None;
col.AutoSizeMode = ColumnAutoSizeMode.None;
superGridControl1.PrimaryGrid.Columns.Add(col);
col = new GridColumn();
col.Name = "核算单元1";
col.HeaderText = "核算单元1";
col.EditorType = typeof(GridLabelXEditControl);
col.FilterMatchType = FilterMatchType.RegularExpressions;
col.CellMergeMode = CellMergeMode.None;
col.AutoSizeMode = ColumnAutoSizeMode.None;
superGridControl1.PrimaryGrid.Columns.Add(col);
col = new GridColumn();
col.Name = "核算单元2";
col.HeaderText = "核算单元2";
col.EditorType = typeof(GridLabelXEditControl);
col.FilterMatchType = FilterMatchType.RegularExpressions;
col.CellMergeMode = CellMergeMode.None;
col.AutoSizeMode = ColumnAutoSizeMode.None;
superGridControl1.PrimaryGrid.Columns.Add(col);
//一行行加载并设置样式 (这种方法比较灵活)
if (DTmain.Rows.Count > 0) { GridRow gr = null; foreach (DataRow dr in DTmain.Rows) { gr = new GridRow(new object[] { (dr["操作名称"]??"").ToString().Trim(), (dr["反馈消息"]??"").ToString().Trim(), (dr["导入条数"]??"").ToString().Trim(), (dr["导入时间段起"]??"").ToString().Trim(), (dr["导入时间段止"]??"").ToString().Trim(), (dr["日志添加时间"]??"").ToString().Trim() }); gr.CellStyles.Default.Font = new Font("微软雅黑", 11); gr.CellStyles.Default.TextColor = Color.White; SPG.PrimaryGrid.Rows.Add(gr); } }
supergridcontrol 样式附加
1 2 3 4 | SPG.DefaultVisualStyles.ColumnHeaderStyles.Default.Background.Color1 = Color.FromArgb(8, 47, 76); //设置列的背景颜色 SPG.DefaultVisualStyles.CellStyles.Default.Alignment = DevComponents.DotNetBar.SuperGrid.Style.Alignment.MiddleCenter //设置单元格文本居中 SPG.DefaultVisualStyles.CellStyles.Default.AllowWrap = Tbool.True; //可自动换行 SPG.DefaultVisualStyles.CellStyles.Selected.AllowWrap = Tbool.True; <br><br> |
注意:设置单元格的相关属性一般CellStyles,行样式用rowStyle
supergridcontrol 在样式上同比其他的控件更加美观,但是在加载速度上不如datageidview 该控件在追求样式的时候丢失了 数据加载速度。
supergridcontrol.cellclik事件中删除所选行 IsDeleted = true;删除行信息,或者supertgridcontrol.PrimaryGrid.Rows.Remove(gr); 这种方式保险点,iddeleted貌似与样式稍微有点冲突,建议用第二种方式。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律