在开发数据DataGrid显示时候通常会遇到自定义DataGrid的列显示, 
 比如经常会提到的, 
 如何单击一行显示某种颜色 
 如何点击到某个单元格不显示编辑框 
 如何自定义一列的显示,比如本来有个标记位flag为0,本行背景显示为白色等等, 
 此类问题归根到底还是要靠 DataGridTableStyle 和 System.Windows.Forms.DataGridColumnStyle 这两个类来完成. 
DataGridColumnStyle  是定义一列显示的样式 
 DataGridTableStyle  是定义整个datagrid的实现样式. 
 dts.GridColumnStyles.Clear();//将column的style清除 
              
             for (int i=0;i<Boxs.Count;i++) 
             { 
                 dts.GridColumnStyles.Add((DataGridTextBoxColumn)Boxs[i]);//把所有的columnstyle附加上去 
             } 
  
              
             Employee_lst.TableStyles.Add(dts);//最后把datatablestyle附加到datagrid上去 
还要注意每一个columnstryl都要对应到列,否则会出错. 
 DatatableStyle也要对应到表. 
 xxx.MappingName=  yyy; 
--------------------------------------------------------------------------------
我们来自己创建一个DataGridColumnStyle 的子类来: 
 这个子类能够解决: 
 如何单击一行显示某种颜色 
 如何点击到某个单元格不显示编辑框 
其实这个子类很简单,主要注意看paint是如何完成的.
using System; 
 using System.Collections; 
 using System.ComponentModel; 
 using System.Drawing; 
 using System.Data; 
 using System.Windows.Forms; 
  
 namespace Stock 
 { 
     /** <summary> 
     /// ButtonColumnStyle2 的摘要说明。 
     /// </summary> 
     public class ButtonColumnStyle2 : System.Windows.Forms.DataGridColumnStyle 
     { 
         /** <summary>  
         /// 必需的设计器变量。 
         /// </summary> 
         private System.ComponentModel.Container components = null; 
         public  delegate void OnClickEventHandler(object sender,int rowIndex); 
         public event OnClickEventHandler OnClick; 
  
         public ButtonColumnStyle2() 
         { 
             // 该调用是 Windows.Forms 窗体设计器所必需的。 
             InitializeComponent(); 
  
             // TODO: 在 InitializeComponent 调用后添加任何初始化 
             Invalidate(); 
              
  
         } 
  
         /** <summary>  
         /// 清理所有正在使用的资源。 
         /// </summary> 
         protected override void Dispose( bool disposing ) 
         { 
             if( disposing ) 
             { 
                 if(components != null) 
                 { 
                     components.Dispose(); 
                 } 
             } 
             base.Dispose( disposing ); 
         }
  
         组件设计器生成的代码#region 组件设计器生成的代码 
  
/** <summary>  
         /// 设计器支持所需的方法 - 不要使用代码编辑器  
         /// 修改此方法的内容。 
         /// </summary> 
         private void InitializeComponent() 
         { 
             components = new System.ComponentModel.Container(); 
         } 
         #endregion 
  
         继承datagridcolumnstyle#region 继承datagridcolumnstyle 
         protected  override void Abort(int rowNum) 
         { 
  
         } 
  
          
         protected  override bool Commit(CurrencyManager dataSource, int rowNum) 
         { 
             return true; 
         } 
  
          
  
         //        protected  override void Edit(CurrencyManager source, int rowNum, Rectangle bounds, bool readOnly) 
         //        { 
         //            base.Edit (source, rowNum, bounds, readOnly); 
         //        } 
         // 
         //         
         // 
         //        protected  override void Edit(CurrencyManager source, int rowNum, Rectangle bounds, bool readOnly, string instantText) 
         //        { 
         //             
         //            //base.Edit (source, rowNum, bounds, readOnly, instantText); 
         //        } 
         // 
         protected  override void Edit(CurrencyManager source, int rowNum, Rectangle bounds, bool readOnly, string instantText, bool cellIsVisible) 
         { 
              
         } 
  
  
         paint#region paint  
         protected  override void Paint(Graphics g, Rectangle bounds, CurrencyManager source, int rowNum) 
         { 
              
         } 
  
         protected  override void Paint(Graphics g, Rectangle bounds, CurrencyManager source, int rowNum, bool alignToRight) 
         { 
  
         } 
         protected  override void Paint(Graphics g, Rectangle bounds, CurrencyManager source, int rowNum, Brush backBrush, Brush foreBrush, bool alignToRight) 
         { 
          
             Brush dbrush = new SolidBrush(System.Drawing.Color.FromArgb(((byte)230), ((byte)236), ((byte)238)));  
             int m_CurrentRow = this.DataGridTableStyle.DataGrid.CurrentRowIndex;  
             if (m_CurrentRow == rowNum)  
             {  
                 g.FillRectangle(dbrush, bounds);  
             }  
             else  
             {  
                 g.FillRectangle(Brushes.White, bounds);  
             }  
             Font font = new Font("宋体",9); 
             DrawText("Assign Stock",font,g,bounds,dbrush); 
             if(OnClick!=null) 
                 OnClick(this,m_CurrentRow); 
  
              
             //base.Paint (g, bounds, source, rowNum, backBrush, foreBrush, alignToRight); 
         } 
  
  
         #endregion 
  
         protected  override int GetPreferredHeight(Graphics g, object value) 
         { 
             return 20; 
         } 
  
         protected  override Size GetPreferredSize(Graphics g, object value) 
         { 
             return new Size (); 
         } 
  
         protected  override int GetMinimumHeight() 
         { 
             return 0; 
         } 
  
         protected override void SetDataGrid(DataGrid value) 
         { 
             base.SetDataGrid (value); 
         } 
         #endregion 
  
         private void DrawText(String Text,Font font,Graphics g,Rectangle bounds,Brush dbrush) 
         { 
             int xMargin,yMargin; 
             xMargin = 2; 
             yMargin = 2 ; 
             float penWidth   =  0.5f; 
             float penWidth2  =  1f; 
  
             g.DrawLine(new Pen(Brushes.DarkGray,penWidth),new Point(bounds.Left+xMargin,+yMargin),new Point(bounds.Left+xMargin,bounds.Bottom-yMargin)); 
             g.DrawLine(new Pen(Brushes.DarkGray,penWidth),new Point(bounds.Right-xMargin,+yMargin),new Point(bounds.Left+xMargin,+yMargin)); 
  
             g.DrawLine(new Pen(Brushes.DimGray,penWidth2),new Point(bounds.Left+xMargin,bounds.Bottom-yMargin),new Point(bounds.Right-xMargin,bounds.Bottom-yMargin)); 
             g.DrawLine(new Pen(Brushes.DimGray,penWidth2),new Point(bounds.Right-xMargin,+yMargin),new Point(bounds.Right-xMargin,bounds.Bottom-yMargin)); 
  
 //            g.FillRectangle(dbrush,bounds.Left+xMargin,+yMargin,bounds.Width-2*xMargin,bounds.Height-2*yMargin); 
  
  
  
             PointF p = new PointF(bounds.Left+2,+5); 
             StringFormat FontFormat = new StringFormat(); 
             FontFormat.Alignment = System.Drawing.StringAlignment.Near; 
  
             g.DrawString(Text, font, Brushes.Black,p,FontFormat); 
  
         } 
     } 
 } 
 
                     
            
        













 
                    

 
                 
                    