在C#winform中自定義DataGrid列標題字體,顏色及背景色

本文介绍了一种通过重画DataGrid来自定义列标题的方法,包括定义通用函数以实现自定义列标题的文字、字体、颜色等,并在DataGrid的Paint事件中调用该函数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

原理:主要通過重畫DataGrid來完成.通過此例也可舉一反三,如 實現自定義行標題,也可用此方法實現.為使代碼具有一定的通用性,特寫了一個較為通用的函數,以便調用.以下是實現步驟,

1.首先定義一個通用的函數,此函數的功能可自定義DataGrid指定索引列的標題文字,標題字體,標題文字顏色,標題背景顏色.

 

         private   void  DrawColHead(DataGrid dg, System.Windows.Forms.PaintEventArgs e, int  index, string  text,Font font,Color foreColor,Color backColor)
        {
            
if ( ! dg.ColumnHeadersVisible)
            {
                
return ;
            }
            
int  colheadheight = 18 ;
            
int  captionheight;
            
int  texty = 6 ;
            
int  nCol =- 1 ;
            
int  colwidth;

            
int  x;        

            
if (dg.RowHeadersVisible)
            {
                x
= dg.RowHeaderWidth;        
            }
            
else
            {
                x
= 0 ;
            }
            
if (dg.CaptionVisible)
            {
                captionheight
= 22 ;
            }
            
else
            {
                captionheight
= 0 ;
            }

            
while (nCol < index && x <= dg.Width)
            {
                nCol
= dg.HitTest (x,captionheight + texty).Column ;
                x
++ ;
            }


            
int  xx = x;
            
while (nCol == index && xx <= dg.Width)
            {
                nCol
= dg.HitTest (xx,captionheight + texty).Column ;
                xx
++ ;
            }

            colwidth
= xx - x - 2 ;

            e.Graphics.FillRectangle(
new  SolidBrush(backColor),x,captionheight + 3 ,colwidth,colheadheight);

            StringFormat   format1   
=     new    StringFormat();
            format1.Trimming
=   StringTrimming.EllipsisWord;
            e.Graphics.DrawString(text,font,
new  SolidBrush(foreColor), new  RectangleF(x,captionheight + texty,colwidth,colheadheight),format1);
        }

  2.在DataGrid的Paint事件中調用函數從而完成自定義列標題.

this.dataGrid1.Paint += new System.Windows.Forms.PaintEventHandler(this.dataGrid1_Paint);

private   void  dataGrid1_Paint( object  sender, System.Windows.Forms.PaintEventArgs e)
        {
            
try
            {
                
if (((DataGrid)sender).DataSource != null )
                {           
                
                    
if (((DataGrid)sender).VisibleColumnCount  > 0 )
                    {       
                        
this .DrawColHead((DataGrid)sender,e, 0 , " 第一列 " , new  Font(FontFamily.GenericSansSerif, 10.0F , FontStyle.Bold),Color.Thistle,Color.GreenYellow);
                        
this .DrawColHead((DataGrid)sender,e, 1 , " 第二列 " , new  Font(FontFamily.GenericSansSerif, 10.0F , FontStyle.Bold),Color.White,Color.Yellow);
                    }
                    

                }
                
base .OnPaint(e);
            }
            
catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

 3.為解決刷新問題,則需要設置雙緩沖,但雙緩沖的設置必須要在控件的構造函數中設置才有效,也就是說必須要自定義控件,這樣一來工程就會很大,考慮到這一點,所以在出現橫向滾動條時加一事件得以解決問題:

this.dataGrid1.Scroll += new System.EventHandler(this.dataGrid1_Scroll);

private   void  dataGrid1_Scroll( object  sender, System.EventArgs e)
{
    
this .dataGrid1.Refresh();
}

 

注:此文是為先前的一個同事寫的.希望能給他一些幫助,同時也給自已一個實踐的機會.共同進度.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值