c# - Text draws out of CellBounds during CellPainting -
i'm overriding cellpainting event datagridview in addin vsts2010. have 2 lines of data , want first line black , second line grey. works fine, sometimes, , no reason can tell, cell contents painted 50 pixels left , 5 pixels should be. example:
the hightlighted filename executionhints.cs can see "nhints.cs"
this happens in 1 vsts session of 3, or @ point during afternoon 1 session. there appears no pattern when happens.
i've tried using e.cellbounds.x , hardcoding "2" x position drawstring still happens. i've attached debugger when happens , values correct, i.e. value == "executionhints.cs", e.cellbounds.x == 1, e.clipbounds.x == 0.
any ideas? suggestions try?
code:
private void grdfiles_cellpainting(object sender, datagridviewcellpaintingeventargs e) { if (grdfiles.columns[e.columnindex].name != clmdisplay.name || e.rowindex < 0) return; var lines = new string[] { "", "" }; solidbrush backcolorbrush; var selected = (datagridviewelementstates.selected & e.state) == datagridviewelementstates.selected; // colour depending on cell contents. if (e.value != null) { lines = system.text.regularexpressions.regex.split(e.value.tostring(), environment.newline); backcolorbrush = selected ? new solidbrush(e.cellstyle.selectionbackcolor) : getbackbrushcolor(lines[1]); } else backcolorbrush = new solidbrush(selected ? e.cellstyle.selectionbackcolor : e.cellstyle.backcolor); using (brush gridbrush = new solidbrush(this.grdfiles.gridcolor)) { using (pen gridlinepen = new pen(gridbrush)) { // erase cell. e.graphics.fillrectangle(backcolorbrush, e.cellbounds); // draw grid lines (only right , bottom lines; // datagridview takes care of others). e.graphics.drawline(gridlinepen, e.cellbounds.left, e.cellbounds.bottom - 1, e.cellbounds.right - 1, e.cellbounds.bottom - 1); e.graphics.drawline(gridlinepen, e.cellbounds.right - 1, e.cellbounds.top, e.cellbounds.right - 1, e.cellbounds.bottom); // draw text content of cell, ignoring alignment. if (e.value != null) { e.graphics.drawstring(lines[0], e.cellstyle.font, brushes.black, 2, e.cellbounds.y + 2, stringformat.genericdefault); e.graphics.drawstring(lines[1], e.cellstyle.font, selected ? brushes.white : brushes.gray, 2, e.cellbounds.y + 18, stringformat.genericdefault); } e.handled = true; } } backcolorbrush.dispose(); }
Comments
Post a Comment