Winform DataGrid设计
一、DataGrid如何动态实现多种风格(Style)之间的转换
DataGrid多用来把数据库中的数据以表格的形式表示出来,初学者的一般做法是用sqlConnection连接数据库后,把数据缓冲到DataSet中,再设定DataGrid的DataSource为此DataSet,这样就轻松的实现了数据的显示。这种方法虽然简单,但是这样有几个缺点:
1.界面不美观。
2.DataGrid最上层的标题与数据库中表格的标题一致。
3.把数据库中的数据没有选择性的全部都显示出来了。
其实DataGrid中TableStyle属性可以解决上面的问题。
首先用System.Windows.Forms.DataGridTableStyle类来设定DataGrid的TableStyle属性,包括显示的背景色、显示的列标题/列类型等;然后建立数据源(可以从数据库直接读取,本文利用DataTable手动创建一些数据作为数据源);最后利用MappingName属性把数据源中的数据同TableStyle中的相关列建立关联即可。
下面的源代码已经在vs2003中测试通过,语句的说明已经标注在程序中了。
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace DataGridBackColor
{
///<summary>
/// MultiStyleForm 的摘要描述。
///</summary>
public class MultiStyleForm : System.Windows.Forms.Form
{
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.DataGrid dataGrid;
///<summary>
///设计工具所需的变数。
///</summary>
private System.ComponentModel.Container components = null;
public MultiStyleForm()
{
//
// 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.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.dataGrid = new System.Windows.Forms.DataGrid();
((System.ComponentModel.ISupportInitialize)(this.dataGrid)).BeginInit();
this.SuspendLayout();
//
// button1
//
this.button1.FlatStyle = System.Windows.Forms.FlatStyle.System;
this.button1.Location = new System.Drawing.Point(40, 16);
this.button1.Name = "button1";
this.button1.TabIndex = 0;
this.button1.Text = "Style1";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.FlatStyle = System.Windows.Forms.FlatStyle.System;
this.button2.Location = new System.Drawing.Point(160, 16);
this.button2.Name = "button2";
this.button2.TabIndex = 1;
this.button2.Text = "Style2";
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// dataGrid
//
this.dataGrid.CaptionText = "DataGrid Example";
this.dataGrid.DataMember = "";
this.dataGrid.Dock = System.Windows.Forms.DockStyle.Bottom;
this.dataGrid.HeaderForeColor = System.Drawing.SystemColors.ControlText;
this.dataGrid.Location = new System.Drawing.Point(0, 54);
this.dataGrid.Name = "dataGrid";
this.dataGrid.Size = new System.Drawing.Size(536, 176);
this.dataGrid.TabIndex = 2;
//
// MultiStyleForm
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 15);
this.ClientSize = new System.Drawing.Size(536, 230);
this.Controls.Add(this.dataGrid);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Name = "MultiStyleForm";
this.Text = "MultiStyleForm";
this.Load += new System.EventHandler(this.MultiStyleForm_Load);
((System.ComponentModel.ISupportInitialize)(this.dataGrid)).EndInit();
this.ResumeLayout(false);
}
#endregion
///<summary>
///应用程序的主进入点。
///</summary>
[STAThread]
static void Main()
{
Application.Run(new MultiStyleForm());
}
#region User Variable
private System.Windows.Forms.DataGridTableStyle TableStyle1;//风格1
private System.Windows.Forms.DataGridBoolColumn TableStyle1Status;//风格1的中Check框
private System.Windows.Forms.DataGridTextBoxColumn TableStyle1TextBox1;//风格1中的TextBox1框
private System.Windows.Forms.DataGridTextBoxColumn TableStyle1TextBox2;//风格1中的TextBox2框
private System.Windows.Forms.DataGridTableStyle TableStyle2;//风格2
private System.Windows.Forms.DataGridTextBoxColumn TableStyle2TextBox1;//风格2中的TextBox1框
private System.Windows.Forms.DataGridTextBoxColumn TableStyle2TextBox2;//风格2中的TextBox2框
private DataTable DataGridTable = null;//用Table作数据源
private DataView DataGridView = null;//用DataView来限定数据源的一些设定
#endregion
#region Init TableStyle 分别初始化风格1和风格2
private void InitTableStyle1()
{
TableStyle1 = new DataGridTableStyle();
TableStyle1Status = new DataGridBoolColumn();
TableStyle1TextBox1 = new DataGridTextBoxColumn();
TableStyle1TextBox2 = new DataGridTextBoxColumn();
//
// TableStyle1
//
this.TableStyle1.AlternatingBackColor = System.Drawing.Color.Wheat;
this.TableStyle1.BackColor = System.Drawing.Color.LightGray;
this.TableStyle1.DataGrid = this.dataGrid;
this.TableStyle1.ForeColor = System.Drawing.Color.MidnightBlue;
this.TableStyle1.GridColumnStyles.AddRange(new System.Windows.Forms.DataGridColumnStyle[] {
this.TableStyle1Status,
this.TableStyle1TextBox1,
this.TableStyle1TextBox2});
this.TableStyle1.GridLineColor = System.Drawing.Color.Black;
this.TableStyle1.HeaderBackColor = System.Drawing.Color.LightBlue;
this.TableStyle1.HeaderForeColor = System.Drawing.SystemColors.ControlText;
this.TableStyle1.MappingName = "";
this.TableStyle1.RowHeadersVisible = false;
this.TableStyle1.SelectionBackColor = System.Drawing.Color.LightSkyBlue;
this.TableStyle1.SelectionForeColor = System.Drawing.Color.WhiteSmoke;
//
// TableStyle1Status
//
this.TableStyle1Status.AllowNull = false;
this.TableStyle1Status.FalseValue = false;
this.TableStyle1Status.HeaderText = "Status";
this.TableStyle1Status.MappingName = "";
this.TableStyle1Status.NullText = "";
this.TableStyle1Status.TrueValue = true;
this.TableStyle1Status.Width = 40;
//
// TableStyle1TextBox1
//
this.TableStyle1TextBox1.Format = "";
this.TableStyle1TextBox1.FormatInfo = null;
this.TableStyle1TextBox1.HeaderText = "TextBox1";
this.TableStyle1TextBox1.MappingName = "";
this.TableStyle1TextBox1.NullText = "";
this.TableStyle1TextBox1.ReadOnly = true;
this.TableStyle1TextBox1.Width = 75;
//
// TableStyle1TextBox2
//
this.TableStyle1TextBox2.Format = "";
this.TableStyle1TextBox2.FormatInfo = null;
this.TableStyle1TextBox2.HeaderText = "TextBox2";
this.TableStyle1TextBox2.MappingName = "";
this.TableStyle1TextBox2.NullText = "";
this.TableStyle1TextBox2.Width = 75;
}
private void InitTableStyle2()
{
TableStyle2 = new DataGridTableStyle();
TableStyle2TextBox1 = new DataGridTextBoxColumn();
TableStyle2TextBox2 = new DataGridTextBoxColumn();
//
// TableStyle2
//
this.TableStyle2.AlternatingBackColor = System.Drawing.Color.Wheat;
this.TableStyle2.BackColor = System.Drawing.Color.LightGray;
this.TableStyle2.DataGrid = this.dataGrid;
this.TableStyle2.ForeColor = System.Drawing.Color.MidnightBlue;
this.TableStyle2.GridColumnStyles.AddRange(new System.Windows.Forms.DataGridColumnStyle[] {
this.TableStyle2TextBox1,
this.TableStyle2TextBox2});
this.TableStyle2.GridLineColor = System.Drawing.Color.Black;
this.TableStyle2.HeaderBackColor = System.Drawing.Color.LightBlue;
this.TableStyle2.HeaderForeColor = System.Drawing.SystemColors.ControlText;
this.TableStyle2.MappingName = "";
this.TableStyle2.RowHeadersVisible = false;
this.TableStyle2.SelectionBackColor = System.Drawing.Color.LightSkyBlue;
this.TableStyle2.SelectionForeColor = System.Drawing.Color.WhiteSmoke;
//
// TableStyle2TextBox1
//
this.TableStyle2TextBox1.Format = "";
this.TableStyle2TextBox1.FormatInfo = null;
this.TableStyle2TextBox1.HeaderText = "TextBox1";
this.TableStyle2TextBox1.MappingName = "";
this.TableStyle2TextBox1.NullText = "";
this.TableStyle2TextBox1.ReadOnly = true;
this.TableStyle2TextBox1.Width = 75;
//
// TableStyle2TextBox2
//
this.TableStyle2TextBox2.Format = "";
this.TableStyle2TextBox2.FormatInfo = null;
this.TableStyle2TextBox2.HeaderText = "TextBox2";
this.TableStyle2TextBox2.MappingName = "";
this.TableStyle2TextBox2.NullText = "";
this.TableStyle2TextBox2.Width = 75;
}
#endregion
#region Init Table View DataGrid 分别初始化Table View DataGrid和为Table创建数据源
private void InitTable()
{
DataGridTable = new DataTable("DataGridTable");//初始化Table
DataColumn[] d = new DataColumn[3];//初始化Table中的列
d[0] = new DataColumn("d0",typeof(bool));
d[1] = new DataColumn("d1",typeof(string));
d[2] = new DataColumn("d2",typeof(string));
DataGridTable.Columns.AddRange(d);//把三列加入Table
TableStyle1.MappingName = "DataGridTable";//风格1绑定数据源
TableStyle1Status.MappingName = "d0";//风格1中的列0绑定数据源中的列0
TableStyle1TextBox1.MappingName = "d1";//风格1中的列1绑定数据源中的列1
TableStyle1TextBox2.MappingName = "d2";//风格1中的列2绑定数据源中的列2
TableStyle2.MappingName = "DataGridTable";//风格2绑定数据源
TableStyle2TextBox1.MappingName = "d2";//风格2中的列1绑定数据源中的列2
TableStyle2TextBox2.MappingName = "d1";//风格2中的列2绑定数据源中的列1
}
private void InitView()
{
DataGridView = new DataView();
DataGridView.Table = DataGridTable;//用View来代表Table,若有多个Table则用DataSet来绑定。
DataGridView.AllowNew = false;//对Table进行必要的限定
}
private void InitDataGrid()
{
dataGrid.DataSource = DataGridView;//用View来设定DataGrid的数据源
}
private void InitData()//为数据源Table添加数据
{
DataRow r = null;
for( int count = 0 ; count < 4 ; count++ )
{
r = DataGridTable.NewRow();
r[0] = false;
r[1] = "TextBox1" + count.ToString();
r[2] = "TextBox2" + count .ToString();
DataGridTable.Rows.Add(r);
}
}
#endregion
private void button1_Click(object sender, System.EventArgs e)
{
//启用风格1
dataGrid.TableStyles.Clear();
dataGrid.TableStyles.Add(TableStyle1);
}
private void button2_Click(object sender, System.EventArgs e)
{
//启动风格2
dataGrid.TableStyles.Clear();
dataGrid.TableStyles.Add(TableStyle2);
}
private void MultiStyleForm_Load(object sender, System.EventArgs e)
{
try
{
InitTableStyle1();
InitTableStyle2();
InitTable();
InitView();
InitDataGrid();
InitData();
}
catch( Exception ex )
{
MessageBox.Show( ex.Message );
}
}
}
}
运行后的画面如下:
原始图面:
启动风格1画面
启动风格2画面
为了能把TableStyle讲清楚,所以本文用了手动的方法来创建。其实vs2003中已经把这个功能整合到IDE中了,通过DataGrid属性栏中的TableStyle属性就可以让IDE自动设置TableStyle的风格了。
二、如何在DataGrid中加入ComboBox控件
C#类库中的DataGrid控件功能非常强大,基本上可以应对一般的表格处理,但是对于特殊的要求,比如在DataGrid中加入单选框(ChechBox)和多选下拉菜单(ComboBox)时就要自己写一些代码了,在Framework1.1中的DataGrid并没有提供这方面的功能。下面介绍一种在DataGrid中加入ComboBox的方法。
1. 首先设置DataGrid的TableStyle
2. 然后利用DataGridTextBoxColumn中的TextBox.Controls属性把ComBoBox加入
3. 最后编写ComboBox的comboBox_SelectedIndexChanged事件,使修改可以保存回DataGrid的相应行。
下面是程序的完整例子,在vs2003中测试通过,代码说明在程序中。添加ComboBox的代码主要在InitDataTable()函数中。
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace DataGridBackColor
{
///<summary>
/// Form1 的摘要描述。
///</summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.DataGrid dataGrid;
private System.Windows.Forms.DataGridTableStyle TableStyle;
private System.Windows.Forms.DataGridBoolColumn Status;
private System.Windows.Forms.DataGridTextBoxColumn DataGridTextBox1;
private System.Windows.Forms.DataGridTextBoxColumn DataGridTextBox2;
private System.Windows.Forms.DataGridTextBoxColumn DataGridComboBox;
///<summary>
///设计工具所需的变数。
///</summary>
private System.ComponentModel.Container components = null;
public Form1()
{
//
// 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()
{
System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(Form1));
this.dataGrid = new System.Windows.Forms.DataGrid();
this.TableStyle = new System.Windows.Forms.DataGridTableStyle();
this.Status = new System.Windows.Forms.DataGridBoolColumn();
this.DataGridTextBox1 = new System.Windows.Forms.DataGridTextBoxColumn();
this.DataGridTextBox2 = new System.Windows.Forms.DataGridTextBoxColumn();
this.DataGridComboBox = new System.Windows.Forms.DataGridTextBoxColumn();
((System.ComponentModel.ISupportInitialize)(this.dataGrid)).BeginInit();
this.SuspendLayout();
//
// dataGrid
//
this.dataGrid.CaptionText = "DataGrid Example";
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(408, 238);
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.DataGridTextBox1,
this.DataGridTextBox2,
this.DataGridComboBox});
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.RowHeadersVisible = false;
this.TableStyle.SelectionBackColor = System.Drawing.Color.LightSkyBlue;
this.TableStyle.SelectionForeColor = System.Drawing.Color.WhiteSmoke;
//
// Status
//
this.Status.AllowNull = false;
this.Status.FalseValue = false;
this.Status.HeaderText = "Status";
this.Status.MappingName = "";
this.Status.NullText = "";
this.Status.NullValue = ((object)(resources.GetObject("Status.NullValue")));
this.Status.TrueValue = true;
this.Status.Width = 40;
//
// DataGridTextBox1
//
this.DataGridTextBox1.Format = "";
this.DataGridTextBox1.FormatInfo = null;
this.DataGridTextBox1.HeaderText = "TextBox1";
this.DataGridTextBox1.MappingName = "";
this.DataGridTextBox1.NullText = "";
this.DataGridTextBox1.ReadOnly = true;
this.DataGridTextBox1.Width = 75;
//
// DataGridTextBox2
//
this.DataGridTextBox2.Format = "";
this.DataGridTextBox2.FormatInfo = null;
this.DataGridTextBox2.HeaderText = "TextBox2";
this.DataGridTextBox2.MappingName = "";
this.DataGridTextBox2.NullText = "";
this.DataGridTextBox2.Width = 75;
//
// DataGridComboBox
//
this.DataGridComboBox.Format = "";
this.DataGridComboBox.FormatInfo = null;
this.DataGridComboBox.HeaderText = "ComboBox";
this.DataGridComboBox.MappingName = "";
this.DataGridComboBox.NullText = "";
this.DataGridComboBox.Width = 75;
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 15);
this.ClientSize = new System.Drawing.Size(408, 238);
this.Controls.Add(this.dataGrid);
this.Name = "Form1";
this.Text = "DataGrid Example";
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 Form1());
}
#region User Variable
private DataTable UserTable = null;
private DataView UserView = null;
#endregion
#region comboBox Event
private void Form1_Load(object sender, System.EventArgs e)
{
try
{
InitDataTable();
InitDataView();
InitDataGrid();
InitData();
}
catch( Exception ex )
{
MessageBox.Show( ex.Message );
}
}
private void comboBox_SelectedIndexChanged(object sender , System.EventArgs e)
{
//将修改写回dataGrid的相应行
if( dataGrid != null )
{
dataGrid[dataGrid.CurrentCell] = ((ComboBox)sender).SelectedItem.ToString();
}
}
#endregion
private void InitDataTable()
{
UserTable = new DataTable("UserTable");
DataColumn[] d = new DataColumn[4];
d[0] = new DataColumn("d0",typeof(bool));
d[1] = new DataColumn("d1",typeof(string));
d[2] = new DataColumn("d2",typeof(string));
d[3] = new DataColumn("d3",typeof(string));
UserTable.Columns.AddRange( d );
TableStyle.MappingName = "UserTable";
Status.MappingName = "d0";
DataGridTextBox1.MappingName = "d1";
DataGridTextBox2.MappingName = "d2";
DataGridComboBox.MappingName = "d3";
//创建一个新的ComboBox控件并设置其基本属性
ComboBox comboBox = new ComboBox();
comboBox.Items.AddRange( new object[]{ "Choose1" , "Choose2" , "Choose3" });
comboBox.DropDownStyle = ComboBoxStyle.DropDownList;
comboBox.Dock = DockStyle.Fill;
DataGridComboBox.TextBox.Controls.Add( comboBox );//将comboBox加入DataGrid的相应行
//关联comboBox的SelectedIndexChanged事件
comboBox.SelectedIndexChanged += new EventHandler(comboBox_SelectedIndexChanged);
}
private void InitDataView()
{
UserView = new DataView();
UserView.Table = UserTable;
UserView.AllowNew = false;
}
private void InitDataGrid()
{
dataGrid.DataSource = UserView;
}
private void InitData()
{
DataRow r = null;
for( int count = 1 ; count < 4 ; count++ )
{
r = UserTable.NewRow();
r[0] = true;
r[1] = "TextBox1" + count.ToString();
r[2] = "TextBox2" + count.ToString();
r[3] = "Choose" + count.ToString();
UserTable.Rows.Add( r );
}
}
}
}
运行结果如下:
在有些时候,DataGrid中不同行的值要以不同的颜色显示出来,比如告警,不同告警的背景色要不同。
三、如何改变DataGrid的背景色
在用DataGrid作为表格显示数据的时候,有时往往需要根据数据的内容来显示不同的颜色,比如作告警列表时,不同的告警要用不同的颜色表示。修改DataGrid背景色的方法很多,这里介绍一种使用DataGrid的TableStyle属性方法来修改背景色。大致过程如下:
1. 自定义DataGridColoredTextBoxColumn类,继承自DataGridTextBoxColumn
2. 重写(override) Paint方法
3. 用DataGridColoredTextBoxColumn类替代程序中的DataGridTextBoxColumn类
代码如下:代码说明详见代码中的注释,该程序在vs2003下测试通过。程序中用到了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 );
}
}
}
运行结果如下:
来源:CSDN