实例代码如下 :
using
System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace DataGridStyle
... {
/**//// <summary>
/// Form1 的摘要说明。
/// </summary>
public class DemoForm : System.Windows.Forms.Form
...{
private System.Windows.Forms.DataGrid dgd;
private System.Windows.Forms.DataGridTableStyle dataGridTableStyle1;
private System.Windows.Forms.DataGridTextBoxColumn dataGridTextBoxColumn1;
private System.Windows.Forms.DataGridTextBoxColumn dataGridTextBoxColumn2;
private System.Windows.Forms.DataGridTextBoxColumn dataGridTextBoxColumn3;
private System.Windows.Forms.DataGridBoolColumn dataGridBoolColumn1;
private System.Windows.Forms.ComboBox cboDepts;
/**//// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
DataTable dt;
public DemoForm()
...{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
/**//// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
...{
if( disposing )
...{
if (components != null)
...{
components.Dispose();
}
}
base.Dispose( disposing );
}
Windows 窗体设计器生成的代码#region Windows 窗体设计器生成的代码
/**//// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
...{
System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(DemoForm));
this.dgd = new System.Windows.Forms.DataGrid();
this.dataGridTableStyle1 = new System.Windows.Forms.DataGridTableStyle();
this.dataGridTextBoxColumn1 = new System.Windows.Forms.DataGridTextBoxColumn();
this.dataGridTextBoxColumn2 = new System.Windows.Forms.DataGridTextBoxColumn();
this.dataGridTextBoxColumn3 = new System.Windows.Forms.DataGridTextBoxColumn();
this.dataGridBoolColumn1 = new System.Windows.Forms.DataGridBoolColumn();
this.cboDepts = new System.Windows.Forms.ComboBox();
((System.ComponentModel.ISupportInitialize)(this.dgd)).BeginInit();
this.SuspendLayout();
//
// dgd
//
this.dgd.DataMember = "";
this.dgd.HeaderForeColor = System.Drawing.SystemColors.ControlText;
this.dgd.Location = new System.Drawing.Point(0, 0);
this.dgd.Name = "dgd";
this.dgd.PreferredRowHeight = 21;
this.dgd.Size = new System.Drawing.Size(400, 136);
this.dgd.TabIndex = 0;
this.dgd.TableStyles.AddRange(new System.Windows.Forms.DataGridTableStyle[] ...{
this.dataGridTableStyle1});
this.dgd.CurrentCellChanged += new System.EventHandler(this.dgd_CurrentCellChanged);
//
// dataGridTableStyle1
//
this.dataGridTableStyle1.DataGrid = this.dgd;
this.dataGridTableStyle1.GridColumnStyles.AddRange(new System.Windows.Forms.DataGridColumnStyle[] ...{
this.dataGridTextBoxColumn1,
this.dataGridTextBoxColumn2,
this.dataGridTextBoxColumn3,
this.dataGridBoolColumn1});
this.dataGridTableStyle1.HeaderForeColor = System.Drawing.SystemColors.ControlText;
this.dataGridTableStyle1.MappingName = "";
this.dataGridTableStyle1.PreferredRowHeight = 21;
//
// dataGridTextBoxColumn1
//
this.dataGridTextBoxColumn1.Alignment = System.Windows.Forms.HorizontalAlignment.Center;
this.dataGridTextBoxColumn1.Format = "";
this.dataGridTextBoxColumn1.FormatInfo = null;
this.dataGridTextBoxColumn1.HeaderText = "编号";
this.dataGridTextBoxColumn1.MappingName = "Id";
this.dataGridTextBoxColumn1.NullText = "暂无";
this.dataGridTextBoxColumn1.Width = 75;
//
// dataGridTextBoxColumn2
//
this.dataGridTextBoxColumn2.Format = "";
this.dataGridTextBoxColumn2.FormatInfo = null;
this.dataGridTextBoxColumn2.HeaderText = "姓名";
this.dataGridTextBoxColumn2.MappingName = "Name";
this.dataGridTextBoxColumn2.NullText = "未知";
//
// dataGridTextBoxColumn3
//
this.dataGridTextBoxColumn3.Format = "";
this.dataGridTextBoxColumn3.FormatInfo = null;
this.dataGridTextBoxColumn3.HeaderText = "部门";
this.dataGridTextBoxColumn3.MappingName = "Dept";
this.dataGridTextBoxColumn3.Width = 75;
//
// dataGridBoolColumn1
//
this.dataGridBoolColumn1.AllowNull = false;
this.dataGridBoolColumn1.FalseValue = false;
this.dataGridBoolColumn1.HeaderText = "婚否";
this.dataGridBoolColumn1.MappingName = "IsMarried";
this.dataGridBoolColumn1.NullValue = ((object)(resources.GetObject("dataGridBoolColumn1.NullValue")));
this.dataGridBoolColumn1.TrueValue = true;
this.dataGridBoolColumn1.Width = 75;
//
// cboDepts
//
this.cboDepts.Cursor = System.Windows.Forms.Cursors.Arrow;
this.cboDepts.Dock = System.Windows.Forms.DockStyle.Top;
this.cboDepts.Items.AddRange(new object[] ...{
"开发部",
"客服部",
"人力资源部"});
this.cboDepts.Location = new System.Drawing.Point(0, 0);
this.cboDepts.Name = "cboDepts";
this.cboDepts.Size = new System.Drawing.Size(400, 20);
this.cboDepts.TabIndex = 1;
this.cboDepts.SelectedIndexChanged += new System.EventHandler(this.cboDepts_SelectedIndexChanged);
//
// DemoForm
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(400, 141);
this.Controls.Add(this.cboDepts);
this.Controls.Add(this.dgd);
this.Name = "DemoForm";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "DataGrid样式";
this.Load += new System.EventHandler(this.DemoForm_Load);
((System.ComponentModel.ISupportInitialize)(this.dgd)).EndInit();
this.ResumeLayout(false);
}
#endregion
/**//// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
...{
Application.Run(new DemoForm());
}
private void DemoForm_Load(object sender, System.EventArgs e)
...{
dt=new DataTable(); //创建内存表
dt.Columns.Add("Id",typeof(int));//定义内存表的字段
dt.Columns.Add("Name",typeof(string));
dt.Columns.Add("Dept",typeof(string));
dt.Columns.Add("IsMarried",typeof(bool));
dt.Columns["IsMarried"].AllowDBNull=false;
dt.Columns["IsMarried"].DefaultValue=true;
DataRow dr=dt.NewRow();
dr["Id"]=9922;
dr["Name"]="Mike";
dr["Dept"]="开发部";
dr["IsMarried"]=false;
dt.Rows.Add(dr);
dgd.DataSource=dt;
dataGridTextBoxColumn3.TextBox.Controls.Add(cboDepts);//在DataGrid中添加下拉选择框
}
private void cboDepts_SelectedIndexChanged(object sender, System.EventArgs e)
...{
if (dgd.CurrentRowIndex>=dt.Rows.Count)
dgd[dgd.CurrentCell]=cboDepts.Text;
else
dt.Rows[dgd.CurrentRowIndex]["Dept"]=cboDepts.Text;
}
private void dgd_CurrentCellChanged(object sender, System.EventArgs e)
...{
if (dgd.CurrentCell.ColumnNumber==2) cboDepts.Text=dgd[dgd.CurrentCell].ToString();
}
}
}
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace DataGridStyle
... {
/**//// <summary>
/// Form1 的摘要说明。
/// </summary>
public class DemoForm : System.Windows.Forms.Form
...{
private System.Windows.Forms.DataGrid dgd;
private System.Windows.Forms.DataGridTableStyle dataGridTableStyle1;
private System.Windows.Forms.DataGridTextBoxColumn dataGridTextBoxColumn1;
private System.Windows.Forms.DataGridTextBoxColumn dataGridTextBoxColumn2;
private System.Windows.Forms.DataGridTextBoxColumn dataGridTextBoxColumn3;
private System.Windows.Forms.DataGridBoolColumn dataGridBoolColumn1;
private System.Windows.Forms.ComboBox cboDepts;
/**//// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
DataTable dt;
public DemoForm()
...{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
/**//// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
...{
if( disposing )
...{
if (components != null)
...{
components.Dispose();
}
}
base.Dispose( disposing );
}
Windows 窗体设计器生成的代码#region Windows 窗体设计器生成的代码
/**//// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
...{
System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(DemoForm));
this.dgd = new System.Windows.Forms.DataGrid();
this.dataGridTableStyle1 = new System.Windows.Forms.DataGridTableStyle();
this.dataGridTextBoxColumn1 = new System.Windows.Forms.DataGridTextBoxColumn();
this.dataGridTextBoxColumn2 = new System.Windows.Forms.DataGridTextBoxColumn();
this.dataGridTextBoxColumn3 = new System.Windows.Forms.DataGridTextBoxColumn();
this.dataGridBoolColumn1 = new System.Windows.Forms.DataGridBoolColumn();
this.cboDepts = new System.Windows.Forms.ComboBox();
((System.ComponentModel.ISupportInitialize)(this.dgd)).BeginInit();
this.SuspendLayout();
//
// dgd
//
this.dgd.DataMember = "";
this.dgd.HeaderForeColor = System.Drawing.SystemColors.ControlText;
this.dgd.Location = new System.Drawing.Point(0, 0);
this.dgd.Name = "dgd";
this.dgd.PreferredRowHeight = 21;
this.dgd.Size = new System.Drawing.Size(400, 136);
this.dgd.TabIndex = 0;
this.dgd.TableStyles.AddRange(new System.Windows.Forms.DataGridTableStyle[] ...{
this.dataGridTableStyle1});
this.dgd.CurrentCellChanged += new System.EventHandler(this.dgd_CurrentCellChanged);
//
// dataGridTableStyle1
//
this.dataGridTableStyle1.DataGrid = this.dgd;
this.dataGridTableStyle1.GridColumnStyles.AddRange(new System.Windows.Forms.DataGridColumnStyle[] ...{
this.dataGridTextBoxColumn1,
this.dataGridTextBoxColumn2,
this.dataGridTextBoxColumn3,
this.dataGridBoolColumn1});
this.dataGridTableStyle1.HeaderForeColor = System.Drawing.SystemColors.ControlText;
this.dataGridTableStyle1.MappingName = "";
this.dataGridTableStyle1.PreferredRowHeight = 21;
//
// dataGridTextBoxColumn1
//
this.dataGridTextBoxColumn1.Alignment = System.Windows.Forms.HorizontalAlignment.Center;
this.dataGridTextBoxColumn1.Format = "";
this.dataGridTextBoxColumn1.FormatInfo = null;
this.dataGridTextBoxColumn1.HeaderText = "编号";
this.dataGridTextBoxColumn1.MappingName = "Id";
this.dataGridTextBoxColumn1.NullText = "暂无";
this.dataGridTextBoxColumn1.Width = 75;
//
// dataGridTextBoxColumn2
//
this.dataGridTextBoxColumn2.Format = "";
this.dataGridTextBoxColumn2.FormatInfo = null;
this.dataGridTextBoxColumn2.HeaderText = "姓名";
this.dataGridTextBoxColumn2.MappingName = "Name";
this.dataGridTextBoxColumn2.NullText = "未知";
//
// dataGridTextBoxColumn3
//
this.dataGridTextBoxColumn3.Format = "";
this.dataGridTextBoxColumn3.FormatInfo = null;
this.dataGridTextBoxColumn3.HeaderText = "部门";
this.dataGridTextBoxColumn3.MappingName = "Dept";
this.dataGridTextBoxColumn3.Width = 75;
//
// dataGridBoolColumn1
//
this.dataGridBoolColumn1.AllowNull = false;
this.dataGridBoolColumn1.FalseValue = false;
this.dataGridBoolColumn1.HeaderText = "婚否";
this.dataGridBoolColumn1.MappingName = "IsMarried";
this.dataGridBoolColumn1.NullValue = ((object)(resources.GetObject("dataGridBoolColumn1.NullValue")));
this.dataGridBoolColumn1.TrueValue = true;
this.dataGridBoolColumn1.Width = 75;
//
// cboDepts
//
this.cboDepts.Cursor = System.Windows.Forms.Cursors.Arrow;
this.cboDepts.Dock = System.Windows.Forms.DockStyle.Top;
this.cboDepts.Items.AddRange(new object[] ...{
"开发部",
"客服部",
"人力资源部"});
this.cboDepts.Location = new System.Drawing.Point(0, 0);
this.cboDepts.Name = "cboDepts";
this.cboDepts.Size = new System.Drawing.Size(400, 20);
this.cboDepts.TabIndex = 1;
this.cboDepts.SelectedIndexChanged += new System.EventHandler(this.cboDepts_SelectedIndexChanged);
//
// DemoForm
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(400, 141);
this.Controls.Add(this.cboDepts);
this.Controls.Add(this.dgd);
this.Name = "DemoForm";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "DataGrid样式";
this.Load += new System.EventHandler(this.DemoForm_Load);
((System.ComponentModel.ISupportInitialize)(this.dgd)).EndInit();
this.ResumeLayout(false);
}
#endregion
/**//// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
...{
Application.Run(new DemoForm());
}
private void DemoForm_Load(object sender, System.EventArgs e)
...{
dt=new DataTable(); //创建内存表
dt.Columns.Add("Id",typeof(int));//定义内存表的字段
dt.Columns.Add("Name",typeof(string));
dt.Columns.Add("Dept",typeof(string));
dt.Columns.Add("IsMarried",typeof(bool));
dt.Columns["IsMarried"].AllowDBNull=false;
dt.Columns["IsMarried"].DefaultValue=true;
DataRow dr=dt.NewRow();
dr["Id"]=9922;
dr["Name"]="Mike";
dr["Dept"]="开发部";
dr["IsMarried"]=false;
dt.Rows.Add(dr);
dgd.DataSource=dt;
dataGridTextBoxColumn3.TextBox.Controls.Add(cboDepts);//在DataGrid中添加下拉选择框
}
private void cboDepts_SelectedIndexChanged(object sender, System.EventArgs e)
...{
if (dgd.CurrentRowIndex>=dt.Rows.Count)
dgd[dgd.CurrentCell]=cboDepts.Text;
else
dt.Rows[dgd.CurrentRowIndex]["Dept"]=cboDepts.Text;
}
private void dgd_CurrentCellChanged(object sender, System.EventArgs e)
...{
if (dgd.CurrentCell.ColumnNumber==2) cboDepts.Text=dgd[dgd.CurrentCell].ToString();
}
}
}
这个实例讲述了内存表的使用以及如何绑定DataGrid,还有就是为DataGrid添加一些其他的控件,如下拉选择框,选择框等等,让DataGrid更加丰富