Winform DataGrid设计三步曲之三
如何改变DataGrid的背景色
在用DataGrid作为表格显示数据的时候,有时往往需要根据数据的内容来显示不同的颜色,比如作告警列表时,不同的告警要用不同的颜色表示。修改DataGrid背景色的方法很多,这里介绍一种使用DataGrid的TableStyle属性方法来修改背景色。大致过程如下:
1. 自定义DataGridColoredTextBoxColumn类,继承自DataGridTextBoxColumn
2. 重写(override) Paint方法
3. 用DataGridColoredTextBoxColumn类替代程序中的DataGridTextBoxColumn类
代码如下:代码说明详见代码中的注释,该程序在vs2003下测试通过。程序中用到了TableStyle,如果对于TableStyle不太清楚,请参看我的第一篇文章
程序中所有关于TableStyle的部分都没有加注释。程序的重点在于DataGridColoredTextBoxColumn 类的构造和Paint方法的重写。
代码如下:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace DataGridBackColor
{
///<summary>
/// BackColorForm 的摘要描述。
///</summary>
public class BackColorForm : System.Windows.Forms.Form
{
//定义DataGridColoredTextBoxColumn类
public class DataGridColoredTextBoxColumn : DataGridTextBoxColumn
{
//利用DataView来传递当前DataGrid中的数据
public DataView dv = null;
//重写Paint
protected override void Paint(Graphics g, Rectangle bounds, CurrencyManager source, int rowNum, Brush backBrush, Brush foreBrush, bool alignToRight)
{
try
{
//判断DataGrid中每行第一列的数据是否为如下的字符串
switch( dv[rowNum].Row[0].ToString() )
{
case "critical" : backBrush = Brushes.Red; break;//改变颜色
case "major" : backBrush = Brushes.Brown; break;//改变颜色
case "minor" : backBrush = Brushes.Orange; break;//改变颜色
case "warning" : backBrush = Brushes.YellowGreen; break;//改变颜色
default : break;
}
}
catch( Exception ex )
{
MessageBox.Show( ex.Message );
}
finally
{
//调用基类的Paint方法
base.Paint (g, bounds, source, rowNum, backBrush, foreBrush, alignToRight);
}
}
}
private System.Windows.Forms.DataGrid dataGrid;
private System.Windows.Forms.DataGridTableStyle TableStyle;
private DataGridColoredTextBoxColumn Status;
private DataGridColoredTextBoxColumn TextBox1;
private DataGridColoredTextBoxColumn TextBox2;
///<summary>
///设计工具所需的变数。
///</summary>
private System.ComponentModel.Container components = null;
public BackColorForm()
{
//
// Windows Form 设计工具支持的必要项
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 呼叫之后加入任何建构函式程序代码
//
}
///<summary>
///清除任何使用中的资源。
///</summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form 设计工具产生的程序代码
///<summary>
///此为设计工具支持所必须的方法 - 请勿使用程序代码编辑器修改
///这个方法的内容。
///</summary>
private void InitializeComponent()
{
this.dataGrid = new System.Windows.Forms.DataGrid();
this.TableStyle = new System.Windows.Forms.DataGridTableStyle();
this.Status = new DataGridColoredTextBoxColumn();
this.TextBox1 = new DataGridColoredTextBoxColumn();
this.TextBox2 = new DataGridColoredTextBoxColumn();
((System.ComponentModel.ISupportInitialize)(this.dataGrid)).BeginInit();
this.SuspendLayout();
//
// dataGrid
//
this.dataGrid.CaptionText = "BackColorForm";
this.dataGrid.DataMember = "";
this.dataGrid.Dock = System.Windows.Forms.DockStyle.Fill;
this.dataGrid.HeaderForeColor = System.Drawing.SystemColors.ControlText;
this.dataGrid.Location = new System.Drawing.Point(0, 0);
this.dataGrid.Name = "dataGrid";
this.dataGrid.Size = new System.Drawing.Size(292, 266);
this.dataGrid.TabIndex = 0;
this.dataGrid.TableStyles.AddRange(new System.Windows.Forms.DataGridTableStyle[] {
this.TableStyle});
//
// TableStyle
//
this.TableStyle.AlternatingBackColor = System.Drawing.Color.Wheat;
this.TableStyle.BackColor = System.Drawing.Color.LightGray;
this.TableStyle.DataGrid = this.dataGrid;
this.TableStyle.ForeColor = System.Drawing.Color.MidnightBlue;
this.TableStyle.GridColumnStyles.AddRange(new System.Windows.Forms.DataGridColumnStyle[] {
this.Status,
this.TextBox1,
this.TextBox2});
this.TableStyle.GridLineColor = System.Drawing.Color.Black;
this.TableStyle.HeaderBackColor = System.Drawing.Color.LightBlue;
this.TableStyle.HeaderForeColor = System.Drawing.SystemColors.ControlText;
this.TableStyle.MappingName = "";
this.TableStyle.SelectionBackColor = System.Drawing.Color.LightSkyBlue;
this.TableStyle.SelectionForeColor = System.Drawing.Color.WhiteSmoke;
//
// Status
//
this.Status.Format = "";
this.Status.FormatInfo = null;
this.Status.HeaderText = "Status";
this.Status.MappingName = "";
this.Status.NullText = "";
this.Status.Width = 75;
//
// TextBox1
//
this.TextBox1.Format = "";
this.TextBox1.FormatInfo = null;
this.TextBox1.HeaderText = "TextBox1";
this.TextBox1.MappingName = "";
this.TextBox1.NullText = "";
this.TextBox1.Width = 75;
//
// TextBox2
//
this.TextBox2.Format = "";
this.TextBox2.FormatInfo = null;
this.TextBox2.HeaderText = "TextBox2";
this.TextBox2.MappingName = "";
this.TextBox2.NullText = "";
this.TextBox2.Width = 75;
//
// BackColorForm
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 15);
this.ClientSize = new System.Drawing.Size(292, 266);
this.Controls.Add(this.dataGrid);
this.Name = "BackColorForm";
this.Text = "BackColorForm";
this.Load += new System.EventHandler(this.Form1_Load);
((System.ComponentModel.ISupportInitialize)(this.dataGrid)).EndInit();
this.ResumeLayout(false);
}
#endregion
///<summary>
///应用程序的主进入点。
///</summary>
[STAThread]
static void Main()
{
Application.Run(new BackColorForm());
}
#region User Variable
private DataTable UserTable = null;
private DataView UserView = null;
#endregion
private void Form1_Load(object sender, System.EventArgs e)
{
try
{
InitDataTable();
InitDataView();
InitDataGrid();
InitData();
}
catch( Exception ex )
{
MessageBox.Show( ex.Message );
}
}
private void InitDataTable()
{
UserTable = new DataTable("UserTable");
DataColumn[] d = new DataColumn[4];
d[0] = new DataColumn("d0",typeof(string));
d[1] = new DataColumn("d1",typeof(string));
d[2] = new DataColumn("d2",typeof(string));
UserTable.Columns.AddRange( d );
TableStyle.MappingName = "UserTable";
Status.MappingName = "d0";
TextBox1.MappingName = "d1";
TextBox2.MappingName = "d2";
}
private void InitDataView()
{
UserView = new DataView();
UserView.Table = UserTable;
UserView.AllowNew = false;
//将DataView赋给DataGridColoredTextBoxColumn类中的DataView变量
Status.dv = UserView;
TextBox1.dv = UserView;
TextBox2.dv = UserView;
}
private void InitDataGrid()
{
dataGrid.DataSource = UserView;
}
private void InitData()
{
DataRow r = null;
//Row 1
r = UserTable.NewRow();
r[0] = "critical";
r[1] = "TextBox11";
r[2] = "TextBox21";
UserTable.Rows.Add( r );
//Row 2
r = UserTable.NewRow();
r[0] = "major";
r[1] = "TextBox12";
r[2] = "TextBox22";
UserTable.Rows.Add( r );
//Row 3
r = UserTable.NewRow();
r[0] = "minor";
r[1] = "TextBox13";
r[2] = "TextBox23";
UserTable.Rows.Add( r );
//Row 4
r = UserTable.NewRow();
r[0] = "warning";
r[1] = "TextBox14";
r[2] = "TextBox24";
UserTable.Rows.Add( r );
}
}
}
运行结果如下:
至此,关于DataGrid的基本用法已经讲完了,DataGrid的功能不仅只是这些,以后我会继续追加关于DataGrid用法的文章。如果是想用ListBox来显示相应的数据并改变背景色的情况,请参见我的另一篇文章
分享到:
相关推荐
WinForm Datagrid结构剖1
WinForm Datagrid结构剖析
C# winform 开发中 DataGrid设计带有筛选功能!含源代码,值得一看!
WinForm Datagrid结构剖
C# 修改winform程序中datagrid列头信息的实际例子源码,内含讲解,和一段实际操作的例子,不错的知识点,供大家学习,我们都知道,再ASP.NET中的datagrid可以设置属性列的列头,但是在winform中却不能,所以提供这个...
使DataGrid的指定行或指定列或指定值为不同条件时以指定的颜色显示突出。 文件名称:DateGridColorSet.dll 制作日期:2005/11/05 开发人员:王波 参考资料:Syncfusion & CSDN 所属项目:JDS ERP 功能介绍:使...
DataGrid(WinForm)单元格变色控件 DataGrid(WinForm)单元格变色控件 DataGrid(WinForm)单元格变色控件
自定义控件,实现在winform窗口中点击查询等待时的页面加载遮罩效果,背景半透明,结果返回前被遮罩的控件不可用。 有问题可留言http://blog.csdn.net/quan278905570
因为业务的需要,有时会使用到自定义运行时窗体设计器Runtime FormDesigner,实现的功能,就是IDE设计器的简化。设想一下,如果可以在程序运行时,再设计一个Form,然后编译代码,那是多么强大呀。
这个是一个可以在winform的grid下显示图片的源代码实例希望对各位有用vb,net写的
搜集了非常漂亮的第三方控件 DataGridView 按钮 combox 漂亮分页控件
C# winform 高级设计三层结构搭建 是专家指导,是深入C#winform 高级设计
《WinForm程序设计与实践》各章节笔记(精要)(包括笔记和代码实例),这是 章节 4.1 winform简介.docx 。
C# Winform 窗体渐变背景色,用画刷绘制背景色.
C#winform DataGridView checkbox复选框 实现单选效果,系统默认的是界面中多选,下午没事,做个单选的效果,同时复习下datagridview的常用知识,包含dantGridVIew的常用操作内容,注意事项,代码有点乱,学习。
winform 最快数据导出--------------------------------------------------
Winform可视化打印模板设计--完整版
C#winform界面特效设计源码,C#winform界面特效设计源码
winform 三层示例--火车车票查询
C# Winform毕业设计源码-音乐播放器