DataGridView 多标题--update.....

using  System;
using  System.Collections.Generic;
using  System.Text;
using  System.Drawing;
using  System.Windows.Forms;
using  System.ComponentModel;

namespace  JControl.WinForm
{
    
public class JDataGrid : DataGridView
    
{

        
//private ScrollableControl m_ScrollableControl = null;
        
        
private List<MergeTitle> m_MergeTitles = null;
        
private List<int> m_EndCols = null;
        
private Color m_BoundColor = Color.Black;
        
public void AddMergeTitle(MergeTitle mergeTitle)
        
{
            
if (m_MergeTitles == null)
            
{
                m_MergeTitles 
= new List<MergeTitle>();
                m_EndCols 
= new List<int>();
            }

            m_MergeTitles.Add(mergeTitle);
            m_EndCols.Add(mergeTitle.EndCol);
        }

        
public void RemoveMergeTitle(int Index)
        
{
            m_MergeTitles.RemoveAt(Index);
            m_EndCols.RemoveAt(Index);
        }

        [Category(
"MergeTitle"), Browsable(false), Description("设置合并单元标题")]
        
public List<MergeTitle> MergeTitles
        
{
            
get{return m_MergeTitles;}
            
set { m_MergeTitles = value; }
        }


        
public Color BoundColor
        
{
            
get{return m_BoundColor;}
            
set{m_BoundColor = value;}
        }

     
        
protected override void OnCellPainting(DataGridViewCellPaintingEventArgs e)
        
{
            
if (e.ColumnIndex > 0 && e.RowIndex == -1 &&
                
this.Columns[e.ColumnIndex] is DataGridViewTextBoxColumn)
            
{

                
if (m_EndCols.Contains(e.ColumnIndex))
                
{
                    
foreach (MergeTitle mergeTitle in m_MergeTitles)
                    
{
                        
if (e.ColumnIndex == mergeTitle.EndCol)
                        
{
                            
this.DrawSingleMergeTitle(mergeTitle, e);
                        }

                    }


                }

            }

            
base.OnCellPainting(e);
        }

        
private void DrawSingleMergeTitle(MergeTitle mergeTitle,DataGridViewCellPaintingEventArgs e)
        
{
            
using
                (
                Brush gridBrush 
= new SolidBrush(m_BoundColor),
                backColorBrush 
= new SolidBrush(e.CellStyle.BackColor),
                ForeColorBrush 
= new SolidBrush(e.CellStyle.ForeColor)
                )
               
{
                    
int sizeWidth = 0;
                    
for (int i = mergeTitle.StartCol; i <= mergeTitle.EndCol; i++)
                    
{
                        sizeWidth 
+= this.Columns[i].Width;
                    }

                    Size size 
= new Size(sizeWidth, this.ColumnHeadersHeight);
                    Point loct 
= new Point(e.CellBounds.X - sizeWidth+e.CellBounds.Width, e.CellBounds.Y);
                    RectangleF rectf 
= new RectangleF((PointF)loct, (SizeF)size);

                    e.Graphics.FillRectangle(backColorBrush, rectf);
                    
//draw line & title
                    using (Pen gridLinePen = new Pen(gridBrush))
                    
{
                       
// LEFT
                        e.Graphics.DrawLine(gridLinePen, rectf.X, rectf.Y, rectf.X, rectf.Bottom);
                        
//RIGHT
                        e.Graphics.DrawLine(gridLinePen, rectf.Right - 1, rectf.Y, rectf.Right - 1, rectf.Bottom);
                        
//TOP
                        e.Graphics.DrawLine(gridLinePen, rectf.X, rectf.Y, rectf.Right, rectf.Y);
                        
//BOTTOM
                        e.Graphics.DrawLine(gridLinePen, rectf.X, rectf.Y + rectf.Bottom - 2, rectf.Right, rectf.Y + rectf.Bottom - 2);

                        
if (m_MergeTitles.Count > 0)
                        
{
                            
float y = 0, ly = 0;
                            
for (int i = 1; i < m_MergeTitles.Count+1; i++)
                            
{
                                y 
= rectf.Y + rectf.Height * i / m_MergeTitles.Count;
                                
//X Center separator
                                e.Graphics.DrawLine(gridLinePen, rectf.X, y, rectf.Right,y);
                                
//Y Center separator
                                if (i < m_MergeTitles.Count)
                                
{
                                    
float xW = rectf.Width / m_MergeTitles[i - 1].Titles.Count;
                                    
float lx = rectf.X;
                                    
for (int j = 1; j <= m_MergeTitles[i - 1].Titles.Count; j++)
                                    
{
                                        
float x = rectf.X + xW * j;
                                        SizeF fSize 
= e.Graphics.MeasureString(
                                      m_MergeTitles[i 
- 1].Titles[j - 1], e.CellStyle.Font);
                                        e.Graphics.DrawString(m_MergeTitles[i 
- 1].Titles[j - 1], e.CellStyle.Font,
                                          ForeColorBrush, lx 
+ (x - lx - fSize.Width) / 2, ly + (y - ly - fSize.Height) / 2,
                                          StringFormat.GenericDefault);

                                        lx 
+= x - lx;

                                        e.Graphics.DrawLine(gridLinePen, x, ly, x, y);

                                      
                                    }

                                }

                                
else //last title
                                {
                                    
float lx = rectf.X;
                                    
for (int k = mergeTitle.StartCol,t=0; k <= mergeTitle.EndCol; k++,t++)
                                    
{

                                        SizeF fSize 
= e.Graphics.MeasureString(
                                        m_MergeTitles[i 
- 1].Titles[t], e.CellStyle.Font);
                                        e.Graphics.DrawString(m_MergeTitles[i 
- 1].Titles[t], e.CellStyle.Font,
                                          ForeColorBrush, lx 
+ (this.Columns[k].Width - fSize.Width) / 2, ly + (y - ly - fSize.Height) / 2, StringFormat.GenericDefault);
                                     
                                        lx 
+= this.Columns[k].Width;
                                        e.Graphics.DrawLine(gridLinePen, lx, ly, lx, y);



                                    }

                                }

                                ly 
= y;
                            }



                    }


                }

            }

            e.Handled 
= true;
        }

    }

}



---------------------------------------------------------------------------------

using  System;
using  System.Collections.Generic;
using  System.Text;
using  System.Collections;
using  System.ComponentModel;

namespace  JControl.WinForm
{
    
public class MergeTitle
    
{
        
private int m_StartCol = -1;
        
private int m_EndCol = -1;
        
private List<string> m_Titles = new List<string>();
        [Browsable(
true), Description("设置合并单元开始列")]
        
public int StartCol
        
{
            
get return m_StartCol; }
            
set { m_StartCol = value; }
        }

        [Browsable(
true), Description("设置合并单元结束列")]
        
public int EndCol 
        
{
            
get return m_EndCol; }
            
set { m_EndCol = value; }
        }

        [Browsable(
true), Description("设置合并单元标题")]
        [DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Content)]
        
public List<string> Titles
        
{
            
get return m_Titles;}
            
set { m_Titles = value; }
        }

        
public void AddTitle(string Title)
        
{
            m_Titles.Add(Title);
        }

        
public void RemoveTitle(int Index)
        
{
            m_Titles.RemoveAt(Index);
        }


    }

}

你可能感兴趣的:(datagridview)