winforms - Creating bottom and right cell borders in C# DataGridView -
i'm looking in creating custom border datagridview
. i'm looking create style column header cells have solid, bottom border , row headers have solid, right border only.
i have managed draw columns headers border adapting this question. however, struggling draw row headers border.
the image below shows have far. can see, column headers have solid black line border. red line i've managed draw row headers, can't seem line extend on rows in table. in other words, how red line draw in row headers cell rows?
this event handler i'm using
private void transitiontable_cellpainting(object sender, datagridviewcellpaintingeventargs e) { if (e.rowindex == -1 && e.columnindex > -1){ e.handled = true; using (brush b = new solidbrush(activetable.defaultcellstyle.backcolor)){ e.graphics.fillrectangle(b, e.cellbounds); } using (pen p = new pen(brushes.black)){ p.dashstyle = system.drawing.drawing2d.dashstyle.solid; e.graphics.drawline(p, new point(0, e.cellbounds.bottom - 1), new point(e.cellbounds.right, e.cellbounds.bottom - 1)); //this `if` statement i've managed working, can't draw line on rows. //it draws in first row if (e.columnindex == 0) { pen b = new pen(brushes.red); e.graphics.drawline(b, new point(e.cellbounds.right - 1, 0), new point(e.cellbounds.right - 1, e.cellbounds.bottom - 1)); } } e.paintcontent(e.clipbounds); } }
your code has 2 issues:
1) start condition applies column header row (-1). need 2 code blocks separate conditions row- , column-headers, maybe this:
private void transitiontable_cellpainting(object sender, datagridviewcellpaintingeventargs e) { using (brush b = new solidbrush(transitiontable.defaultcellstyle.backcolor)) e.graphics.fillrectangle(b, e.cellbounds); e.paintcontent(e.clipbounds); if (e.rowindex == -1) // column header { e.graphics.drawline(pens.black, 0, e.cellbounds.bottom - 1, e.cellbounds.right, e.cellbounds.bottom - 1); } if (e.columnindex == -1) // row header (*) { e.graphics.drawline(pens.red, e.cellbounds.right - 1, 0, e.cellbounds.right - 1, e.cellbounds.bottom - 1); } e.handled = true; }
or avoid owner-drawing regular cells wrapping whole block in or-condition
like :
if (e.rowindex == -1 || e.columnindex = -1) { code above in here! }
2) code , screenshot if don't have rowheaders
, though, , draw line in first data column. if want, ignore last remark , change condition (*)
if (e.columnindex == 0)
Comments
Post a Comment