本文介绍了如何合并在DataGridView的C#特定的列标题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想采取拖列的列标题文本。你会怎么做呢?
筛选



| FULL名称|



|第一|最后一个






我秀第一和放大器;最后在一列


解决方案

 公共部分类MergeRowHeaderForm5:表
{
酒店的公共MergeRowHeaderForm5()
{
的InitializeComponent();
}

私人无效MergeRowHeaderForm5_Load(对象发件人,EventArgs五)
{
this.dataGridView1.Columns.Add(JanWin,赢);
this.dataGridView1.Columns.Add(JanLoss,损失);
this.dataGridView1.Columns.Add(FebWin,赢);
this.dataGridView1.Columns.Add(FebLoss,损失);
this.dataGridView1.Columns.Add(的MarWin,赢);
this.dataGridView1.Columns.Add(MarLoss,损失);
this.dataGridView1.Columns.Add(AprWin,赢);
this.dataGridView1.Columns.Add(AprLoss,损失);
this.dataGridView1.Columns.Add(MayWin,赢);
this.dataGridView1.Columns.Add(MayLoss,损失);

为(INT J = 0; J< this.dataGridView1.ColumnCount; J ++)
{
this.dataGridView1.Columns [J] .WIDTH = 45;
}

this.dataGridView1.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing;
this.dataGridView1.ColumnHeadersHeight = this.dataGridView1.ColumnHeadersHeight * 2;
this.dataGridView1.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.BottomCenter;
this.dataGridView1.CellPainting + =新DataGridViewCellPaintingEventHandler(dataGridView1_CellPainting);
this.dataGridView1.Paint + =新PaintEventHandler(dataGridView1_Paint);
this.dataGridView1.Scroll + =新ScrollEventHandler(dataGridView1_Scroll);
this.dataGridView1.ColumnWidthChanged + =新DataGridViewColumnEventHandler(dataGridView1_ColumnWidthChanged);
}


私人无效dataGridView1_ColumnWidthChanged(对象发件人,DataGridViewColumnEventArgs E)
{
矩形rtHeader = this.dataGridView1.DisplayRectangle;
rtHeader.Height = this.dataGridView1.ColumnHeadersHeight / 2;
this.dataGridView1.Invalidate(rtHeader);
}

私人无效dataGridView1_Scroll(对象发件人,ScrollEventArgs E)
{
矩形rtHeader = this.dataGridView1.DisplayRectangle;
rtHeader.Height = this.dataGridView1.ColumnHeadersHeight / 2;
this.dataGridView1.Invalidate(rtHeader);
}

私人无效dataGridView1_Paint(对象发件人,PaintEventArgs的E)
{
的String [] monthes = {员工信息,,, ,};
表示(中间体J = 0; J&小于10)
{
矩形R1 = this.dataGridView1.GetCellDisplayRectangle(J,-1,真);
INT W2 = this.dataGridView1.GetCellDisplayRectangle第(j + 1,-1,真).WIDTH;
R1.X + = 1;
R1.Y + = 1;
r1.Width = r1.Width + W2 - 2;
r1.Height = r1.Height / 2 - 2;
e.Graphics.FillRectangle(新SolidBrush(this.dataGridView1.ColumnHeadersDefaultCellStyle.BackColor),R1);
格式的StringFormat =新的StringFormat();
format.Alignment = StringAlignment.Center;
format.LineAlignment = StringAlignment.Center;
e.Graphics.DrawString(monthes [J / 2],
this.dataGridView1.ColumnHeadersDefaultCellStyle.Font,
新SolidBrush(this.dataGridView1.ColumnHeadersDefaultCellStyle.ForeColor),
R1 ,
格式);
J + = 2;
}
}

私人无效dataGridView1_CellPainting(对象发件人,DataGridViewCellPaintingEventArgs E)
{
如果(e.RowIndex == -1放大器;&安培; e.ColumnIndex -1个)
{
矩形R2 = e.CellBounds;
r2.Y + = e.CellBounds.Height / 2;
r2.Height = e.CellBounds.Height / 2;
e.PaintBackground(R2,真);
e.PaintContent(R2);
e.Handled = TRUE;
}
}
}




I would like to take a column header text for tow columns. How would you do this? Like this

| FULL NAME |

| first | last


I show First & last in one column

解决方案
public partial class MergeRowHeaderForm5 : Form
{
    public MergeRowHeaderForm5()
    {
        InitializeComponent();
    }

    private void MergeRowHeaderForm5_Load(object sender, EventArgs e)
    {
        this.dataGridView1.Columns.Add("JanWin", "Win");
        this.dataGridView1.Columns.Add("JanLoss", "Loss");
        this.dataGridView1.Columns.Add("FebWin", "Win");
        this.dataGridView1.Columns.Add("FebLoss", "Loss");
        this.dataGridView1.Columns.Add("MarWin", "Win");
        this.dataGridView1.Columns.Add("MarLoss", "Loss");
        this.dataGridView1.Columns.Add("AprWin", "Win");
        this.dataGridView1.Columns.Add("AprLoss", "Loss");
        this.dataGridView1.Columns.Add("MayWin", "Win");
        this.dataGridView1.Columns.Add("MayLoss", "Loss");

        for (int j = 0; j < this.dataGridView1.ColumnCount; j++)
        {
            this.dataGridView1.Columns[j].Width = 45;
        }

        this.dataGridView1.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing;
        this.dataGridView1.ColumnHeadersHeight = this.dataGridView1.ColumnHeadersHeight * 2;
        this.dataGridView1.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.BottomCenter;
        this.dataGridView1.CellPainting += new DataGridViewCellPaintingEventHandler(dataGridView1_CellPainting);
        this.dataGridView1.Paint += new PaintEventHandler(dataGridView1_Paint);
        this.dataGridView1.Scroll += new ScrollEventHandler(dataGridView1_Scroll);
        this.dataGridView1.ColumnWidthChanged += new DataGridViewColumnEventHandler(dataGridView1_ColumnWidthChanged);
    }


    private void dataGridView1_ColumnWidthChanged(object sender, DataGridViewColumnEventArgs e)
    {
        Rectangle rtHeader = this.dataGridView1.DisplayRectangle;
        rtHeader.Height = this.dataGridView1.ColumnHeadersHeight / 2;
        this.dataGridView1.Invalidate(rtHeader);
    }

    private void dataGridView1_Scroll(object sender, ScrollEventArgs e)
    {
        Rectangle rtHeader = this.dataGridView1.DisplayRectangle;
        rtHeader.Height = this.dataGridView1.ColumnHeadersHeight / 2;
        this.dataGridView1.Invalidate(rtHeader);
    }

    private void dataGridView1_Paint(object sender, PaintEventArgs e)
    {
        string[] monthes = { "Emp Info", "", "", "", "" };
        for (int j = 0; j < 10; )
        {
            Rectangle r1 = this.dataGridView1.GetCellDisplayRectangle(j, -1, true);
            int w2 = this.dataGridView1.GetCellDisplayRectangle(j + 1, -1, true).Width;
            r1.X += 1;
            r1.Y += 1;
            r1.Width = r1.Width + w2 - 2;
            r1.Height = r1.Height / 2 - 2;
            e.Graphics.FillRectangle(new SolidBrush(this.dataGridView1.ColumnHeadersDefaultCellStyle.BackColor), r1);
            StringFormat format = new StringFormat();
            format.Alignment = StringAlignment.Center;
            format.LineAlignment = StringAlignment.Center;
            e.Graphics.DrawString(monthes[j / 2],
            this.dataGridView1.ColumnHeadersDefaultCellStyle.Font,
            new SolidBrush(this.dataGridView1.ColumnHeadersDefaultCellStyle.ForeColor),
            r1,
            format);
            j += 2;
        }
    }

    private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
    {
        if (e.RowIndex == -1 && e.ColumnIndex > -1)
        {
            Rectangle r2 = e.CellBounds;
            r2.Y += e.CellBounds.Height / 2;
            r2.Height = e.CellBounds.Height / 2;
            e.PaintBackground(r2, true);
            e.PaintContent(r2);
            e.Handled = true;
        }
    }
}

这篇关于如何合并在DataGridView的C#特定的列标题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-21 05:51