C# cannot change DefaultCellStyle.WrapMode of a DataGridView -



C# cannot change DefaultCellStyle.WrapMode of a DataGridView -

i using datagridview, bound datatable. defaultcellstyle.wrapmode set false , working way want it. however, want utilize custom cellpainting (code below), supposed to, wrapmode no longer respected , longer strings wrapped when added "url" column of datagridview1.

private void datagridview1_cellpainting(object sender, datagridviewcellpaintingeventargs e) { if (e.rowindex < 0) return; if (e.columnindex == datagridview1.columns["url"].index) { if ((e.state & datagridviewelementstates.selected) == datagridviewelementstates.selected) return; e.cellstyle.wrapmode = datagridviewtristate.false; rectangle rect = new rectangle(e.cellbounds.x, e.cellbounds.y, e.cellbounds.width - 1, e.cellbounds.height - 1); using (system.drawing.drawing2d.lineargradientbrush lgb = new system.drawing.drawing2d.lineargradientbrush(rect, color.white, color.honeydew, 0f)) { e.graphics.fillrectangle(lgb, rect); } if (e.value == null) return; using (system.drawing.pen pen = new system.drawing.pen(datagridview1.gridcolor)) { e.graphics.drawrectangle(pen, e.cellbounds.x - 1, e.cellbounds.y - 1, e.cellbounds.width, e.cellbounds.height); } stringformat sf = new stringformat(); sf.linealignment = stringalignment.center; sf.alignment = stringalignment.near; using (system.drawing.brush valuebrush = new solidbrush(e.cellstyle.forecolor)) { e.graphics.drawstring(e.value.tostring(), e.cellstyle.font, valuebrush, rect, sf); } e.handled = true; } }

i have tried adding next line:

datagridview1.defaultcellstyle.wrapmode = datagridviewtristate.false;

it not work, have tried

e.cellstyle.wrapmode = datagridviewtristate.false;

it not work either.

how utilize custom cellpainting , set defaultcellstyle.wrapmode false?

instead of e.graphics.drawstring(e.value.tostring(), e.cellstyle.font, valuebrush, rect, sf); seek utilize textrenderer.

var textformatflag = textformatflags.singleline | textformatflags.horizontalcenter | textformatflags.verticalcenter; textrenderer.drawtext(e.graphics, e.value.tostring(), e.cellstyle.font, rect, e.cellstyle.forecolor, textformatflag);

if content of cell wider cell add together flag well:

textformatflags.endellipsis

to end '...' :)

for more info options available here.

c#

Comments

Popular posts from this blog

web services - java.lang.NoClassDefFoundError: Could not initialize class net.sf.cglib.proxy.Enhancer -

Accessing MATLAB's unicode strings from C -

javascript - mongodb won't find my schema method in nested container -