Friday, 15 June 2012

c# - datagridview multicolored text -



c# - datagridview multicolored text -

i trying show multicolored text in datagridviewcell , i'm there. found little bit problem double clicking column divider.

when double click column divider, portion of cell background color not painted properly. forgetting code? please suggest me.

here code

using system; using system.collections.generic; using system.componentmodel; using system.data; using system.drawing; using system.linq; using system.text; using system.windows.forms; namespace multicoloredgridviewtest { public partial class form1 : form { public form1() { initializecomponent(); } private void form1_load(object sender, eventargs e) { datagridview1.rows.add("niaz,rahim,azme,niaz,rahim,azme", "123,677,111", "dhaka,dhaka,dhaka"); } private void datagridview1_cellpainting(object sender, datagridviewcellpaintingeventargs e) { if (e.rowindex >= -1) { // rectangle newrect = new rectangle(e.cellbounds.x + 1, e.cellbounds.y + 1, e.cellbounds.width - 1, e.cellbounds.height - 1); e.paint(e.clipbounds, datagridviewpaintparts.all & ~datagridviewpaintparts.contentforeground); stringformat sf = new stringformat(); sf.alignment = stringalignment.center; sf.linealignment = stringalignment.center; point startpoint = e.cellbounds.location; if (e.value != null) { string[] datalist = e.value.tostring().split(','); foreach (string info in datalist) { sizef tmpsize = e.graphics.measurestring(data.trim(), e.cellstyle.font); rectangle txtregion = new rectangle(startpoint, new size((int)(tmpsize.width + 3), e.cellbounds.height)); color clr = new color(); if (data.trim().contains("rahim")) clr = color.red; else clr = color.black; using (solidbrush br = new solidbrush(clr)) { e.graphics.drawstring(data.trim(), e.cellstyle.font, br, txtregion, stringformat.genericdefault); } startpoint = new point(startpoint.x+txtregion.width+1, e.cellbounds.location.y); } e.handled = true; } } } private void button1_click(object sender, eventargs e) { string[] cell = textbox1.text.split(','); string value = datagridview1.rows[int.parse(cell[0])].cells[int.parse(cell[1])].value.tostring(); label1.text = value; } private void datagridview1_columndividerdoubleclick(object sender, datagridviewcolumndividerdoubleclickeventargs e) { datagridview1.columns[e.columnindex].dividerwidth = datagridview1.currentcell.value.tostring().length + 10; datagridview1.invalidate(); } } }

try expand columns , apply painting.

something this:

private void btnautoresizecontactnamecolumn_click(object sender, eventargs e) { // perform auto resize on contact column this.ultragrid1.displaylayout.bands[0].columns["contactname"].performautoresize(); }

check out link

c# .net winforms datagridview

No comments:

Post a Comment