在列标题上绘制矩形 [英] paint rectangle on column header

查看:102
本文介绍了在列标题上绘制矩形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在datagridview的列标题上绘制矩形,但在滚动到右侧时它会消失,如图中所示(scroll.png)



这里是我的代码

  Rectangle r1; 
void dataGridView1_Paint(object sender,PaintEventArgs e)
{
string [] monthes = {APPLE,MANGO,CHERRY,GRAPES,PINEAPPLE};
for(int j = 0; j {
r1 = this.dataGridView1.GetCellDisplayRectangle(j,-1,true);
int w2 = this.dataGridView1.GetCellDisplayRectangle(j + 1,-1,true).Width;
r1.X + = -2;
r1.Y + = 30;
r1.Width = r1.Width + w2 - 1;
r1.Height = r1.Height / 3 - 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.DrawRectangle(new Pen(Color.Black),r1);
e.Graphics.DrawString(monthes [j / 2],this.dataGridView1.ColumnHeadersDefaultCellStyle.Font,new SolidBrush(this.dataGridView1.ColumnHeadersDefaultCellStyle.ForeColor),r1,format);

j + = 2;



void dataGridView1_Scroll(object sender,ScrollEventArgs e)
{
Rectangle rtHeader = this.dataGridView1.DisplayRectangle;
rtHeader.Y + = 0;
rtHeader.Height = this.dataGridView1.ColumnHeadersHeight;
this.dataGridView1.Invalidate(rtHeader);
}


解决方案

负责你所描述行为的行是 this.dataGridView1.Invalidate(rtHeader);



两种解决方案:


  • 删除上述行。
  • 或者将rectanges放入单元格中第一行就可以),而不是在标题中。


I'm painting rectangle on the column headers in datagridview but on scrolling to right it disappears as in the picture (scroll.png)

here is my code

Rectangle r1;
void dataGridView1_Paint(object sender, PaintEventArgs e)
{
    string[] monthes = { "APPLE", "MANGO", "CHERRY", "GRAPES", "PINEAPPLE" };
    for (int j = 0; j < this.dataGridView1.ColumnCount; )
    {
        r1 = this.dataGridView1.GetCellDisplayRectangle(j, -1, true);
        int w2 = this.dataGridView1.GetCellDisplayRectangle(j + 1, -1, true).Width;
        r1.X += -2;
        r1.Y += 30;
        r1.Width = r1.Width + w2 - 1;
        r1.Height = r1.Height / 3 - 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.DrawRectangle(new Pen(Color.Black), r1);
        e.Graphics.DrawString(monthes[j / 2], this.dataGridView1.ColumnHeadersDefaultCellStyle.Font, new SolidBrush(this.dataGridView1.ColumnHeadersDefaultCellStyle.ForeColor), r1, format);

        j += 2;
    }
}

void dataGridView1_Scroll(object sender, ScrollEventArgs e)
{
    Rectangle rtHeader = this.dataGridView1.DisplayRectangle;
    rtHeader.Y += 0;
    rtHeader.Height = this.dataGridView1.ColumnHeadersHeight;
    this.dataGridView1.Invalidate(rtHeader);
}

解决方案

I have tested your code and the line responsible for the behaviour you are describing is this.dataGridView1.Invalidate(rtHeader);

Thus you have two solutions:

  • Deleting the aforementioned line.
  • Or putting the rectanges in the cells (in the first row, would be fine), rather than in the headers.

这篇关于在列标题上绘制矩形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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