要想在DataGrid中運用ComboBox,DateTimePicker,我可以對System.Windows.Forms.DataGridColumnStyle進行重寫來做到....
一、將ComboBox綁定與DataGridColumnStyle ,然後對DataGridColumnStyle進行重寫...
代碼與下:
using System;
using System.Windows.Forms;
using System.Drawing;
namespace XUIControls
{
/// <summary>
/// Summary description for DataGridComboBoxColumnStyle.
/// </summary>
public class DataGridComboBoxColumnStyle:System.Windows.Forms.DataGridColumnStyle
{
private ComboBox X_ComboBox=new ComboBox();
private bool IsEditing;
public DataGridComboBoxColumnStyle():base()
{
//
// TODO: Add constructor logic here
//
X_ComboBox.Visible=false;
}
protected override void Abort(int rowNum)
{
IsEditing=false;
X_ComboBox.Click-=new EventHandler(this.ComboBoxValueChanged);
Invalidate();
}
protected override bool Commit(CurrencyManager dataSource, int rowNum)
{
X_ComboBox.Bounds=Rectangle.Empty;
X_ComboBox.Click-=new EventHandler(this.ComboBoxValueChanged);
if (!IsEditing)
return true;
IsEditing=false;
try
{
string value=X_ComboBox.Text;
SetColumnValueAtRow(dataSource,rowNum,value);
}
catch(Exception)
{
Abort(rowNum);
return false;
}
Invalidate();
return true;
}
protected override void Edit(CurrencyManager source, int rowNum, Rectangle bounds, bool readOnly,string instantText,bool cellIsVisible)
{
string value=(string) GetColumnValueAtRow(source,rowNum);
if (cellIsVisible)
{
X_ComboBox.Bounds=new Rectangle
(bounds.X+2,bounds.Y+2,bounds.Width-4,bounds.Height-4);
X_ComboBox.Text=value;
X_ComboBox.Visible=true;
X_ComboBox.Click += new EventHandler(this.ComboBoxValueChanged);
}
else
{
X_ComboBox.Text=value;
X_ComboBox.Visible=false;
}
if (X_ComboBox.Visible)
DataGridTableStyle.DataGrid.Invalidate(bounds);
}
protected override void Paint(Graphics g,Rectangle bounds,CurrencyManager source,int rowNum)
{
Paint(g,bounds,source,rowNum,false);
}
protected override void Paint(
Graphics g,
Rectangle bounds,
CurrencyManager source,
int rowNum,
bool alignToRight)
{
Paint(
g,bounds,
source,
rowNum,
Brushes.Red,
Brushes.Blue,
alignToRight);
}
protected override void Paint(
Graphics g,
Rectangle bounds,
CurrencyManager source,
int rowNum,
Brush backBrush,
Brush foreBrush,
bool alignToRight)
{
string date=(string)
GetColumnValueAtRow(source,rowNum);
Rectangle rect=bounds;
g.FillRectangle(backBrush,rect);
rect.Offset(0,2);
rect.Height-=2;
g.DrawString(date,this.DataGridTableStyle.DataGrid.Font,foreBrush,rect);
//g.DrawString(date,this.DataGridTableStyle.DataGrid.Font,foreBrush,rect.X,rect.Y); //ok
}
protected override Size GetPreferredSize(Graphics g,object value)
{
return new Size(100,24);
}
protected override int GetMinimumHeight()
{
return 24;
}
protected override int GetPreferredHeight(Graphics g,object value)
{
return 24;
}
protected override void SetDataGridInColumn(DataGrid value)
{
base.SetDataGridInColumn(value);
if (X_ComboBox.Parent != null)
{
X_ComboBox.Parent.Controls.Remove(X_ComboBox);
}
if (value != null)
{
value.Controls.Add(X_ComboBox);
}
}
private void ComboBoxValueChanged(object sender,EventArgs e)
{
this.IsEditing=true;
base.ColumnStartedEditing(X_ComboBox);
}
public void AddItem(string StrItemName)
{
X_ComboBox.Items.Add(StrItemName);
}
}
}
二、將DateTimePicker綁定與DataGridColumnStyle ,然後對DataGridColumnStyle進行重寫...
代碼與下:
using System;
using System.Windows.Forms;
using System.Drawing;
namespace XUIControls
{
/// <summary>
/// Summary description for DataGridDateTimePickerColumnStyle.
/// </summary>
public class DataGridDateTimePickerColumnStyle:DataGridColumnStyle
{
private DateTimePicker X_DateTimePicker=new DateTimePicker();
private bool IsEditing;
public DataGridDateTimePickerColumnStyle():base()
{
//
// TODO: Add constructor logic here
//
X_DateTimePicker.Visible=false;
}
protected override void Abort(int rowNum)
{
IsEditing=false;
X_DateTimePicker.ValueChanged-=new EventHandler(this.TimePickerValueChanged);
Invalidate();
}
protected override bool Commit(CurrencyManager dataSource,int rowNum)
{
X_DateTimePicker.Bounds=Rectangle.Empty;
X_DateTimePicker.ValueChanged-=new EventHandler(this.TimePickerValueChanged);
if(!IsEditing)
{
return true;
}
IsEditing=false;
try
{
System.DateTime values=X_DateTimePicker.Value;
SetColumnValueAtRow(dataSource,rowNum,values);
}
catch(Exception)
{
Abort(rowNum);
return false;
}
Invalidate();
return true;
}
protected override void Edit(CurrencyManager source,int rowNum,Rectangle bounds,bool ReadOnly,string instantText,bool cellIsVisible)
{
System.DateTime value=(DateTime)GetColumnValueAtRow(source,rowNum);
if (cellIsVisible)
{
X_DateTimePicker.Bounds=new Rectangle
(bounds.X+2,bounds.Y+2,bounds.Width-4,bounds.Height-4);
X_DateTimePicker.Value=value;
X_DateTimePicker.Visible=true;
X_DateTimePicker.ValueChanged+=new EventHandler(this.TimePickerValueChanged);
}
else
{
X_DateTimePicker.Value=value;
X_DateTimePicker.Visible=false;
}
if (X_DateTimePicker.Visible)
DataGridTableStyle.DataGrid.Invalidate(bounds);
}
protected override void Paint(Graphics g,Rectangle bounds,CurrencyManager source,int rowNum)
{
Paint(g,bounds,source,rowNum,false);
}
protected override void Paint(Graphics g,Rectangle bounds,CurrencyManager source,int rowNum,bool alignToRight)
{
Paint(g,bounds,source,rowNum,Brushes.Red,Brushes.Blue,alignToRight);
}
protected override void Paint(Graphics g,Rectangle bounds,CurrencyManager source,int rowNum,Brush backBrush,Brush foreBrush,bool alignToRight)
{
System.DateTime date=(DateTime)
GetColumnValueAtRow(source,rowNum);
Rectangle rect=bounds;
g.FillRectangle(backBrush,rect);
rect.Offset(0,2);
rect.Height-=2;
g.DrawString(date.ToString("d"),this.DataGridTableStyle.DataGrid.Font,foreBrush,rect);
}
protected override Size GetPreferredSize(Graphics g,object value)
{
return new Size(100,24);
}
protected override int GetMinimumHeight()
{
return 24;
}
protected override int GetPreferredHeight(Graphics g,object value)
{
return 24;
}
protected override void SetDataGridInColumn(DataGrid value)
{
base.SetDataGridInColumn(value);
if (X_DateTimePicker.Parent !=null)
{
X_DateTimePicker.Parent.Controls.Remove(X_DateTimePicker);
}
if(value !=null)
{
value.Controls.Add(X_DateTimePicker);
}
}
private void TimePickerValueChanged(object sender,EventArgs e)
{
this.IsEditing=true;
base.ColumnStartedEditing(X_DateTimePicker);
}
}
}
● 到此,兩個重寫的UIControl都已完成,現在新建一個Form,在Form中放置一個DataGird,我們將在DataGrid中同時實現運用ComboBox,DateTimePicker,CheckBox…..
代碼部分:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace XForms
{
/// <summary>
/// Summary description for Form_Diary.
/// </summary>
public class Form_Diary : System.Windows.Forms.Form
{
private My_Diarys.UserControls.UserControl_Header userControl_Header1;
private System.Windows.Forms.DataGrid dataGrid1;
private DataTable namesDataTable;
private XUIControls.XButton button1;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
public Form_Diary()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent call
//
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.userControl_Header1 = new My_Diarys.UserControls.UserControl_Header();
this.dataGrid1 = new System.Windows.Forms.DataGrid();
this.button1 = new XUIControls.XButton();
((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit();
this.SuspendLayout();
//
// userControl_Header1
//
this.userControl_Header1.Location = new System.Drawing.Point(0, 0);
this.userControl_Header1.Name = "userControl_Header1";
this.userControl_Header1.Size = new System.Drawing.Size(162, 28);
this.userControl_Header1.TabIndex = 0;
//
// dataGrid1
//
this.dataGrid1.DataMember = "";
this.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText;
this.dataGrid1.Location = new System.Drawing.Point(10, 102);
this.dataGrid1.Name = "dataGrid1";
this.dataGrid1.Size = new System.Drawing.Size(528, 198);
this.dataGrid1.TabIndex = 1;
//
// button1
//
this.button1.Location = new System.Drawing.Point(408, 378);
this.button1.Name = "button1";
this.button1.TabIndex = 2;
this.button1.Text = "button1";
//
// Form_Diary
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(792, 547);
this.Controls.Add(this.button1);
this.Controls.Add(this.dataGrid1);
this.Controls.Add(this.userControl_Header1);
this.Name = "Form_Diary";
this.RightToLeft = System.Windows.Forms.RightToLeft.No;
this.Text = "Form_Diary";
this.Load += new System.EventHandler(this.Form_Diary_Load);
((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit();
this.ResumeLayout(false);
}
#endregion
[STAThread]
static void Main()
{
Application.Run(new XForms.Form_Diary());
}
private void AddData()
{
DataRow dRow = namesDataTable.NewRow();
dRow["Name"] = "Name 1";
dRow["State"] = "xxx";
dRow["Date"] = new DateTime(2004,11,24);
namesDataTable.Rows.Add(dRow);
dRow = namesDataTable.NewRow();
dRow["Name"] = "Name 2";
dRow["Date"] = new DateTime(2004,11,24);
dRow["State"] ="xxx";
namesDataTable.Rows.Add(dRow);
dRow = namesDataTable.NewRow();
dRow["Name"] = "Name 3";
dRow["State"] = "xxx";
dRow["Date"] = new DateTime(2004,11,24);
namesDataTable.Rows.Add(dRow);
dRow = namesDataTable.NewRow();
dRow["Name"] = "Name 4";
dRow["State"] = "xxx";
dRow["Date"] = new DateTime(2004,11,24);
namesDataTable.Rows.Add(dRow);
dRow = namesDataTable.NewRow();
dRow["Name"] = "Name 5";
dRow["State"] = "xxx";
dRow["Date"] = new DateTime(2004,11,24);
namesDataTable.Rows.Add(dRow);
namesDataTable.AcceptChanges();
}
private void AddGridStyle()
{
DataGridTableStyle myGridStyle = new DataGridTableStyle();
myGridStyle.MappingName = "NamesTable";
DataGridTextBoxColumn nameColumnStyle =
new DataGridTextBoxColumn();
nameColumnStyle.MappingName = "Name";
nameColumnStyle.HeaderText= "Name";
myGridStyle.GridColumnStyles.Add(nameColumnStyle);
XUIControls.DataGridDateTimePickerColumnStyle dateColumnStyle =
new XUIControls.DataGridDateTimePickerColumnStyle();
dateColumnStyle.MappingName = "Date";
dateColumnStyle.HeaderText= "Date";
myGridStyle.GridColumnStyles.Add(dateColumnStyle);
XUIControls.DataGridComboBoxColumnStyle cmbStyle=new XUIControls.DataGridComboBoxColumnStyle();
cmbStyle.MappingName = "State";
cmbStyle.HeaderText = "State";
cmbStyle.Width = 100;
cmbStyle.AddItem("xxx");
cmbStyle.AddItem("1111");
myGridStyle.GridColumnStyles.Add(cmbStyle);
this.dataGrid1.TableStyles.Add(myGridStyle);
}
private void Form_Diary_Load(object sender, System.EventArgs e)
{
namesDataTable=new DataTable("NamesTable");
namesDataTable.Columns.Add(new DataColumn("Name"));
namesDataTable.Columns.Add(new DataColumn("State"));
// namesDataTable.Columns.Add(new DataColumn("Date"));
DataColumn dateColumn = new DataColumn
("Date", typeof(System.DateTime));
namesDataTable.Columns.Add(dateColumn);
DataSet namesDataSet = new DataSet();
namesDataSet.Tables.Add(namesDataTable);
this.dataGrid1.DataSource = namesDataSet;
this.dataGrid1.DataMember = "NamesTable";
AddGridStyle();
AddData();
}
}
}