winforms - paint rectangle on column header in c# -


enter image description here

i have painted datagridview column header , called repaint event in scroll event ,but not seems repaint . text in painted rectangle gets scettered ( see second image) here code,

 void datagridview1_scroll(object sender, scrolleventargs e)  {      rectangle rtheader = this.datagridview1.displayrectangle;      rtheader.y += 0;      rtheader.height = this.datagridview1.columnheadersheight;  }  rectangle r1;  void datagridview1_paint(object sender, painteventargs e)  {      string[] monthes = { "apple", "mango", "cherry", "grapes", "pineapple" };      (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;      }      string[] year = { "january", "february", "march", "april", "may" };      //for (int = 0; < this.datagridview1.columncount; )      //{            rectangle rec = this.datagridview1.getcelldisplayrectangle(0, -1, true);            int wid = this.datagridview1.getcelldisplayrectangle(1, -1, true).width;            rec.x += -2;            rec.y += 1;            rec.width = this.datagridview1.columns.getcolumnswidth(datagridviewelementstates.visible);            rec.height = rec.height / 3 - 2;            e.graphics.fillrectangle(new solidbrush(this.datagridview1.columnheadersdefaultcellstyle.backcolor), rec);            stringformat frm = new stringformat();            frm.alignment = stringalignment.center;            frm.linealignment = stringalignment.center;            e.graphics.drawrectangle(new pen(color.black), rec);            e.graphics.drawstring("favourite fruits", new font("times new roman", 16, fontstyle.regular), new solidbrush(color.cornflowerblue), rec, frm);  } 

enter image description here

private 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();     } 

Comments

Popular posts from this blog

php - Calling a template part from a post -

Firefox SVG shape not printing when it has stroke -

How to mention the localhost in android -