winform - 合并datagridview标头 [英] winform - merging datagridview headers

查看:300
本文介绍了winform - 合并datagridview标头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我使用这段代码:

  void dataGridView1_Paint(object sender,PaintEventArgs e)
{
Rectangle r1 = dataGridView1.GetCellDisplayRectangle(2,-1,true);
Rectangle r2 = dataGridView1.GetCellDisplayRectangle(3,-1,true);

r1.X + = 1;
r1.Y + = 2;
r1.Width + = r2.Width - 2;
r1.Height - = 6;

using(SolidBrush br = new SolidBrush(dataGridView1.ColumnHeadersDefaultCellStyle.BackColor))
{
e.Graphics.FillRectangle(br,r1);


//绘制文本
使用(SolidBrush br = new SolidBrush(this.dataGridView1.ColumnHeadersDefaultCellStyle.ForeColor))
{
StringFormat sf = new StringFormat
{
LineAlignment = StringAlignment.Center,
Alignment = StringAlignment.Center
};
e.Graphics.DrawString(merged header,dataGridView1.ColumnHeadersDefaultCellStyle.Font,br,r1,sf);
}
}

滚动网格之前。一切都很好,但滚动后,标题文字更改为垃圾文字。
请检查快照



我很感激有人可以帮助我找到一个很好的解决方案。



ali.mz

解决方案

我相信最简单的方法是在每次datagridview滚动时使合并的标题的单元格无效。您需要在Scroll事件中添加一个处理程序:

  dataGridView1.Scroll + = new System.Windows.Forms.ScrollEventHandler this.dataGridView1_Scroll); 

以下是滚动事件处理程序实现:

  private void dataGridView1_Scroll(object sender,ScrollEventArgs e)
{
Rectangle rect = Rectangle.Union(
dataGridView1.GetCellDisplayRectangle(2,-1,true ),
dataGridView1.GetCellDisplayRectangle(3,-1,true));
dataGridView1.Invalidate(rect);
}

希望这有助于,


I have a problem with merge the datagridview headers in winForm.

I using this code :

void dataGridView1_Paint(object sender, PaintEventArgs e)
    {
        Rectangle r1 = dataGridView1.GetCellDisplayRectangle(2, -1, true);
        Rectangle r2 = dataGridView1.GetCellDisplayRectangle(3, -1, true);

        r1.X += 1;
        r1.Y += 2;
        r1.Width += r2.Width - 2;
        r1.Height -= 6;

        using (SolidBrush br = new SolidBrush(dataGridView1.ColumnHeadersDefaultCellStyle.BackColor))
        {
            e.Graphics.FillRectangle(br, r1);
        }

        //draw text
        using (SolidBrush br = new SolidBrush(this.dataGridView1.ColumnHeadersDefaultCellStyle.ForeColor))
        {
            StringFormat sf = new StringFormat
                                  {
                                      LineAlignment = StringAlignment.Center,
                                      Alignment = StringAlignment.Center
                                  };
            e.Graphics.DrawString("merged header", dataGridView1.ColumnHeadersDefaultCellStyle.Font, br, r1, sf);
        }
    }

before scrolling the grid . everything is fine but after scrolling the header text changed to garbage text. please check the snapshot .

I would appreciate it someone could help me to find a good solution.

ali.mz

解决方案

I believe the easiest way would be to invalidate merged header's cells every time the datagridview is scrolled. You would need to add a handler to the Scroll event:

dataGridView1.Scroll += new System.Windows.Forms.ScrollEventHandler(this.dataGridView1_Scroll);

Below is scroll event handler implementation:

private void dataGridView1_Scroll(object sender, ScrollEventArgs e)
{
    Rectangle rect = Rectangle.Union(
        dataGridView1.GetCellDisplayRectangle(2, -1, true), 
        dataGridView1.GetCellDisplayRectangle(3, -1, true));
    dataGridView1.Invalidate(rect);
}

hope this helps, regards

这篇关于winform - 合并datagridview标头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆