最简单的方法在DataGrid中自定义样式列(通过继承可以在Datagrid中使用下拉列表框,日期控件等)
在.Net中使用最普遍的表格控件当属DataGrid,初看起来它的功能不强,实际上它是由很多子控件堆砌而成的,如果熟悉其基本事件的处理过程,则可以做出很强大功能的表格控件。 
这里是自定义样式列的基类,继承的DataGridTextBoxColumn类
 using System;
using System; 
 using System.Collections;
using System.Collections; 
 using System.ComponentModel;
using System.ComponentModel; 
 using System.Drawing;
using System.Drawing; 
 using System.Data;
using System.Data; 
 using System.Windows.Forms;
using System.Windows.Forms; 
 
 
 namespace System.Windows.Forms
namespace System.Windows.Forms 
 {
{ 
 /// <summary>
    /// <summary> 
 /// DataGrid自定义列的基类
    /// DataGrid自定义列的基类 
 /// </summary>
    /// </summary> 
 public abstract class DataGridCustomColumn : System.Windows.Forms.DataGridTextBoxColumn
    public abstract class DataGridCustomColumn : System.Windows.Forms.DataGridTextBoxColumn 
 {
    { 
 protected Control ctrl;
        protected Control ctrl; 
 CurrencyManager _source;
        CurrencyManager _source; 
 int _iRowNum;
        int _iRowNum; 
 
 
 public DataGridCustomColumn()
        public DataGridCustomColumn() 
 {
        { 
 }
        } 
 
 
 protected void Init()
        protected void Init() 
 {
        { 
 ctrl.Enter+=new EventHandler(ctrl_Enter);
            ctrl.Enter+=new EventHandler(ctrl_Enter); 
 ctrl.Leave+=new EventHandler(ctrl_Leave);
            ctrl.Leave+=new EventHandler(ctrl_Leave); 
 ctrl.Visible=false;
            ctrl.Visible=false; 
 }
        } 
 
         
 private void ctrl_Enter(object sender, EventArgs e)
        private void ctrl_Enter(object sender, EventArgs e) 
 {
        { 
 if(ReadOnly)
            if(ReadOnly) 
 {
            { 
 Control control=(Control)sender;
                Control control=(Control)sender; 
 control.Visible=false;
                control.Visible=false; 
 }
            } 
 this.DataGridTableStyle.DataGrid.Select(this.DataGridTableStyle.DataGrid.CurrentRowIndex);
            this.DataGridTableStyle.DataGrid.Select(this.DataGridTableStyle.DataGrid.CurrentRowIndex); 
 
 
 }
        } 
 
 
 private void ctrl_Leave(object sender, EventArgs e)
        private void ctrl_Leave(object sender, EventArgs e) 
 {
        { 
 try
            try 
 {
            { 
 this.SetColumnValueAtRow(_source,_iRowNum ,Value);
                this.SetColumnValueAtRow(_source,_iRowNum ,Value); 
 }
            } 
 catch{}
            catch{} 
 ctrl.Visible = false;
            ctrl.Visible = false; 
 }
        } 
 public virtual object Value
        public virtual object Value 
 {
        { 
 get{return null;}
            get{return null;} 
 set{}
            set{} 
 }
        } 
 protected override void Edit(CurrencyManager source, int rowNum, Rectangle bounds, bool readOnly, string instantText, bool cellIsVisible)
        protected override void Edit(CurrencyManager source, int rowNum, Rectangle bounds, bool readOnly, string instantText, bool cellIsVisible) 
 {
        { 
 if (!this.DataGridTableStyle.DataGrid.Controls.Contains(ctrl)) this.DataGridTableStyle.DataGrid.Controls.Add(ctrl);
            if (!this.DataGridTableStyle.DataGrid.Controls.Contains(ctrl)) this.DataGridTableStyle.DataGrid.Controls.Add(ctrl); 
 _iRowNum = rowNum;
            _iRowNum = rowNum; 
 _source  = source;
            _source  = source; 
 ctrl.Bounds = bounds;
            ctrl.Bounds = bounds; 
 
             
 try
            try 
 {
            { 
 Value = this.GetColumnValueAtRow(_source, _iRowNum);
                Value = this.GetColumnValueAtRow(_source, _iRowNum);             
 }
            } 
 catch{}
            catch{} 
 ctrl.Visible = true;
            ctrl.Visible = true; 
 ctrl.Focus();
            ctrl.Focus(); 
 }
        } 
 public Control control
        public Control control 
 {
        { 
 get
            get 
 {
            { 
 return ctrl;
                return ctrl; 
 }
            } 
 }
        } 
 }
    } 
 }
} 
下拉列表框样式类
 
 
 namespace System.Windows.Forms
namespace System.Windows.Forms 
 {
{ 
 /// <summary>
    /// <summary> 
 /// 下拉列表框样式列
    /// 下拉列表框样式列 
 /// </summary>
    /// </summary> 
 public class DataGridComboBoxColumn : System.Windows.Forms.DataGridCustomColumn
    public class DataGridComboBoxColumn : System.Windows.Forms.DataGridCustomColumn 
 {
    { 
 ComboBox _cbo=new ComboBox();
        ComboBox _cbo=new ComboBox(); 
 
 
 public DataGridComboBoxColumn()
        public DataGridComboBoxColumn() 
 {
        { 
 ctrl=_cbo;
            ctrl=_cbo; 
 Init();
            Init(); 
 }
        } 
 public override object Value
        public override object Value 
 {
        { 
 get
            get 
 {
            { 
 return _cbo.Text;
                return _cbo.Text; 
 }
            } 
 set
            set 
 {
            { 
 try
                try 
 {
                { 
 _cbo.Text = (string)value;
                    _cbo.Text = (string)value; 
 }
                } 
 catch{}
                catch{} 
 }
            } 
 }
        } 
 public void FillComboBox(string [] comboList)
        public void FillComboBox(string [] comboList) 
 {
        { 
 _cbo.Items.AddRange(comboList);
            _cbo.Items.AddRange(comboList); 
 }
        } 
 }
    } 
 } 
日期样式类
} 
日期样式类 
 namespace System.Windows.Forms
namespace System.Windows.Forms 
 {
{ 
 /// <summary>
    /// <summary> 
 /// 时间日期样式列
    /// 时间日期样式列 
 /// </summary>
    /// </summary> 
 public class DataGridDatetimeColumn: System.Windows.Forms.DataGridCustomColumn
    public class DataGridDatetimeColumn: System.Windows.Forms.DataGridCustomColumn 
 {
    { 
 DateTimePicker _dt;
        DateTimePicker _dt; 
 public DataGridDatetimeColumn()
        public DataGridDatetimeColumn() 
 {
        { 
 _dt=new DateTimePicker();
            _dt=new DateTimePicker(); 
 ctrl=_dt;
            ctrl=_dt; 
 Init();
            Init(); 
 }
        } 
 public override object Value
        public override object Value 
 {
        { 
 get
            get 
 {
            { 
 return _dt.Value;
                return _dt.Value; 
 }
            } 
 set
            set 
 {
            { 
 try
                try 
 {
                { 
 _dt.Value = (DateTime)value;
                    _dt.Value = (DateTime)value; 
 }
                } 
 catch{}
                catch{} 
 }
            } 
 }
        } 
 
 
 }
    } 
 }
} 
其它的样式可以自己继承DataGridCustomColumn类,只需要重写一个基类方法,可谓是最简单的在Datagrid中实现自定义样式列的办法。
下面是测试效果的代码
 public void GridComboBoxSample()
public void GridComboBoxSample() 
 {
        { 
 //生成一个DataTable(表)
            //生成一个DataTable(表) 
 DataTable t = new DataTable("PersonInfo");
            DataTable t = new DataTable("PersonInfo"); 
 t.Columns.Add("Name",typeof(string));
            t.Columns.Add("Name",typeof(string)); 
 t.Columns.Add("Address",typeof(string));
            t.Columns.Add("Address",typeof(string)); 
 t.Columns.Add("birthday",typeof(DateTime));
            t.Columns.Add("birthday",typeof(DateTime)); 
 t.Rows.Add(new string[]{"yinzx","China","2000-01-01"});
            t.Rows.Add(new string[]{"yinzx","China","2000-01-01"}); 
 
             
 //生成一个DataGridTableStyle(表样式)
            //生成一个DataGridTableStyle(表样式) 
 DataGridTableStyle ts = new DataGridTableStyle();
            DataGridTableStyle ts = new DataGridTableStyle(); 
 
             
 DataGridTextBoxColumn c1 = new DataGridTextBoxColumn();
            DataGridTextBoxColumn c1 = new DataGridTextBoxColumn(); 
 DataGridComboBoxColumn c2 = new DataGridComboBoxColumn();
            DataGridComboBoxColumn c2 = new DataGridComboBoxColumn();   
 DataGridDatetimeColumn c3=new DataGridDatetimeColumn();
            DataGridDatetimeColumn c3=new DataGridDatetimeColumn(); 
 c1.MappingName = c1.HeaderText  = "Name";
            c1.MappingName = c1.HeaderText  = "Name"; 
 c2.MappingName = c2.HeaderText  = "Address";
            c2.MappingName = c2.HeaderText  = "Address"; 
 c3.MappingName=c3.HeaderText="birthday";
            c3.MappingName=c3.HeaderText="birthday"; 
 c2.FillComboBox(new string[]{"China","Canada","France"});
            c2.FillComboBox(new string[]{"China","Canada","France"}); 
 ts.MappingName = "PersonInfo";
            ts.MappingName = "PersonInfo"; 
 ts.GridColumnStyles.Add(c1);
            ts.GridColumnStyles.Add(c1); 
 ts.GridColumnStyles.Add(c2);
            ts.GridColumnStyles.Add(c2); 
 ts.GridColumnStyles.Add(c3);
            ts.GridColumnStyles.Add(c3); 
 
             
 //把表及表样式绑定到grd上
            //把表及表样式绑定到grd上 
 dataGrid1.TableStyles.Add(ts);
            dataGrid1.TableStyles.Add(ts); 
 dataGrid1.DataSource = t;
            dataGrid1.DataSource = t; 
 }
} 
注:把ReadOnly属性设置为真,可以实现VB中MSHFlex中的整行选择的效果。
本文参考了网上流行的增加下拉列表框的代码,因被转载的次数太多,无法得知原作者,在这里向原作者致歉。
注:把ReadOnly属性设置为真,可以实现VB中MSHFlex中的整行选择的效果。本文参考了网上流行的增加下拉列表框的代码,因被转载的次数太多,无法得知原作者,在这里向原作者致歉。 
这里是自定义样式列的基类,继承的DataGridTextBoxColumn类
 using System;
using System;  using System.Collections;
using System.Collections;  using System.ComponentModel;
using System.ComponentModel;  using System.Drawing;
using System.Drawing;  using System.Data;
using System.Data;  using System.Windows.Forms;
using System.Windows.Forms;  
  namespace System.Windows.Forms
namespace System.Windows.Forms  {
{  /// <summary>
    /// <summary>  /// DataGrid自定义列的基类
    /// DataGrid自定义列的基类  /// </summary>
    /// </summary>  public abstract class DataGridCustomColumn : System.Windows.Forms.DataGridTextBoxColumn
    public abstract class DataGridCustomColumn : System.Windows.Forms.DataGridTextBoxColumn  {
    {  protected Control ctrl;
        protected Control ctrl;  CurrencyManager _source;
        CurrencyManager _source;  int _iRowNum;
        int _iRowNum;  
  public DataGridCustomColumn()
        public DataGridCustomColumn()  {
        {  }
        }  
  protected void Init()
        protected void Init()  {
        {  ctrl.Enter+=new EventHandler(ctrl_Enter);
            ctrl.Enter+=new EventHandler(ctrl_Enter);  ctrl.Leave+=new EventHandler(ctrl_Leave);
            ctrl.Leave+=new EventHandler(ctrl_Leave);  ctrl.Visible=false;
            ctrl.Visible=false;  }
        }  
          private void ctrl_Enter(object sender, EventArgs e)
        private void ctrl_Enter(object sender, EventArgs e)  {
        {  if(ReadOnly)
            if(ReadOnly)  {
            {  Control control=(Control)sender;
                Control control=(Control)sender;  control.Visible=false;
                control.Visible=false;  }
            }  this.DataGridTableStyle.DataGrid.Select(this.DataGridTableStyle.DataGrid.CurrentRowIndex);
            this.DataGridTableStyle.DataGrid.Select(this.DataGridTableStyle.DataGrid.CurrentRowIndex);  
  }
        }  
  private void ctrl_Leave(object sender, EventArgs e)
        private void ctrl_Leave(object sender, EventArgs e)  {
        {  try
            try  {
            {  this.SetColumnValueAtRow(_source,_iRowNum ,Value);
                this.SetColumnValueAtRow(_source,_iRowNum ,Value);  }
            }  catch{}
            catch{}  ctrl.Visible = false;
            ctrl.Visible = false;  }
        }  public virtual object Value
        public virtual object Value  {
        {  get{return null;}
            get{return null;}  set{}
            set{}  }
        }  protected override void Edit(CurrencyManager source, int rowNum, Rectangle bounds, bool readOnly, string instantText, bool cellIsVisible)
        protected override void Edit(CurrencyManager source, int rowNum, Rectangle bounds, bool readOnly, string instantText, bool cellIsVisible)  {
        {  if (!this.DataGridTableStyle.DataGrid.Controls.Contains(ctrl)) this.DataGridTableStyle.DataGrid.Controls.Add(ctrl);
            if (!this.DataGridTableStyle.DataGrid.Controls.Contains(ctrl)) this.DataGridTableStyle.DataGrid.Controls.Add(ctrl);  _iRowNum = rowNum;
            _iRowNum = rowNum;  _source  = source;
            _source  = source;  ctrl.Bounds = bounds;
            ctrl.Bounds = bounds;  
              try
            try  {
            {  Value = this.GetColumnValueAtRow(_source, _iRowNum);
                Value = this.GetColumnValueAtRow(_source, _iRowNum);              }
            }  catch{}
            catch{}  ctrl.Visible = true;
            ctrl.Visible = true;  ctrl.Focus();
            ctrl.Focus();  }
        }  public Control control
        public Control control  {
        {  get
            get  {
            {  return ctrl;
                return ctrl;  }
            }  }
        }  }
    }  }
} 下拉列表框样式类
 
  namespace System.Windows.Forms
namespace System.Windows.Forms  {
{  /// <summary>
    /// <summary>  /// 下拉列表框样式列
    /// 下拉列表框样式列  /// </summary>
    /// </summary>  public class DataGridComboBoxColumn : System.Windows.Forms.DataGridCustomColumn
    public class DataGridComboBoxColumn : System.Windows.Forms.DataGridCustomColumn  {
    {  ComboBox _cbo=new ComboBox();
        ComboBox _cbo=new ComboBox();  
  public DataGridComboBoxColumn()
        public DataGridComboBoxColumn()  {
        {  ctrl=_cbo;
            ctrl=_cbo;  Init();
            Init();  }
        }  public override object Value
        public override object Value  {
        {  get
            get  {
            {  return _cbo.Text;
                return _cbo.Text;  }
            }  set
            set  {
            {  try
                try  {
                {  _cbo.Text = (string)value;
                    _cbo.Text = (string)value;  }
                }  catch{}
                catch{}  }
            }  }
        }  public void FillComboBox(string [] comboList)
        public void FillComboBox(string [] comboList)  {
        {  _cbo.Items.AddRange(comboList);
            _cbo.Items.AddRange(comboList);  }
        }  }
    }  }
}  namespace System.Windows.Forms
namespace System.Windows.Forms  {
{  /// <summary>
    /// <summary>  /// 时间日期样式列
    /// 时间日期样式列  /// </summary>
    /// </summary>  public class DataGridDatetimeColumn: System.Windows.Forms.DataGridCustomColumn
    public class DataGridDatetimeColumn: System.Windows.Forms.DataGridCustomColumn  {
    {  DateTimePicker _dt;
        DateTimePicker _dt;  public DataGridDatetimeColumn()
        public DataGridDatetimeColumn()  {
        {  _dt=new DateTimePicker();
            _dt=new DateTimePicker();  ctrl=_dt;
            ctrl=_dt;  Init();
            Init();  }
        }  public override object Value
        public override object Value  {
        {  get
            get  {
            {  return _dt.Value;
                return _dt.Value;  }
            }  set
            set  {
            {  try
                try  {
                {  _dt.Value = (DateTime)value;
                    _dt.Value = (DateTime)value;  }
                }  catch{}
                catch{}  }
            }  }
        }  
  }
    }  }
} 其它的样式可以自己继承DataGridCustomColumn类,只需要重写一个基类方法,可谓是最简单的在Datagrid中实现自定义样式列的办法。
下面是测试效果的代码
 public void GridComboBoxSample()
public void GridComboBoxSample()  {
        {  //生成一个DataTable(表)
            //生成一个DataTable(表)  DataTable t = new DataTable("PersonInfo");
            DataTable t = new DataTable("PersonInfo");  t.Columns.Add("Name",typeof(string));
            t.Columns.Add("Name",typeof(string));  t.Columns.Add("Address",typeof(string));
            t.Columns.Add("Address",typeof(string));  t.Columns.Add("birthday",typeof(DateTime));
            t.Columns.Add("birthday",typeof(DateTime));  t.Rows.Add(new string[]{"yinzx","China","2000-01-01"});
            t.Rows.Add(new string[]{"yinzx","China","2000-01-01"});  
              //生成一个DataGridTableStyle(表样式)
            //生成一个DataGridTableStyle(表样式)  DataGridTableStyle ts = new DataGridTableStyle();
            DataGridTableStyle ts = new DataGridTableStyle();  
              DataGridTextBoxColumn c1 = new DataGridTextBoxColumn();
            DataGridTextBoxColumn c1 = new DataGridTextBoxColumn();  DataGridComboBoxColumn c2 = new DataGridComboBoxColumn();
            DataGridComboBoxColumn c2 = new DataGridComboBoxColumn();    DataGridDatetimeColumn c3=new DataGridDatetimeColumn();
            DataGridDatetimeColumn c3=new DataGridDatetimeColumn();  c1.MappingName = c1.HeaderText  = "Name";
            c1.MappingName = c1.HeaderText  = "Name";  c2.MappingName = c2.HeaderText  = "Address";
            c2.MappingName = c2.HeaderText  = "Address";  c3.MappingName=c3.HeaderText="birthday";
            c3.MappingName=c3.HeaderText="birthday";  c2.FillComboBox(new string[]{"China","Canada","France"});
            c2.FillComboBox(new string[]{"China","Canada","France"});  ts.MappingName = "PersonInfo";
            ts.MappingName = "PersonInfo";  ts.GridColumnStyles.Add(c1);
            ts.GridColumnStyles.Add(c1);  ts.GridColumnStyles.Add(c2);
            ts.GridColumnStyles.Add(c2);  ts.GridColumnStyles.Add(c3);
            ts.GridColumnStyles.Add(c3);  
              //把表及表样式绑定到grd上
            //把表及表样式绑定到grd上  dataGrid1.TableStyles.Add(ts);
            dataGrid1.TableStyles.Add(ts);  dataGrid1.DataSource = t;
            dataGrid1.DataSource = t;  }
} 注:把ReadOnly属性设置为真,可以实现VB中MSHFlex中的整行选择的效果。
本文参考了网上流行的增加下拉列表框的代码,因被转载的次数太多,无法得知原作者,在这里向原作者致歉。
 
                    
                     
                    
                 
                    
                


 
     
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号