C# DataGridView Display image and text in same cell -



C# DataGridView Display image and text in same cell -

i have requirement need add together image , text same column dynamically. have followed many examples none of them working. referred next getting cast error while referring above article. unable cast object of type system.windows.forms.datagridviewimagecell type datagridviewcustom.textandimagecell.

http://akhaliq.com/?p=82

can help on this?

i didn't find code on link share, i'll give code used before.

1- first create new class name it textandimagecolumn.cs :

this class code :

using system; using system.collections.generic; using system.linq; using system.text; using system.windows.forms; using system.drawing; namespace tradegrid { public class textandimagecolumn : datagridviewtextboxcolumn { private image imagevalue; private size imagesize; public textandimagecolumn() { this.celltemplate = new textandimagecell(); } public override object clone() { textandimagecolumn c = base.clone() textandimagecolumn; c.imagevalue = this.imagevalue; c.imagesize = this.imagesize; homecoming c; } public image image { { homecoming this.imagevalue; } set { if (this.image != value) { this.imagevalue = value; this.imagesize = value.size; if (this.inheritedstyle != null) { padding inheritedpadding = this.inheritedstyle.padding; this.defaultcellstyle.padding = new padding(imagesize.width, inheritedpadding.top, inheritedpadding.right, inheritedpadding.bottom); } } } } private textandimagecell textandimagecelltemplate { { homecoming this.celltemplate textandimagecell; } } internal size imagesize { { homecoming imagesize; } } } public class textandimagecell : datagridviewtextboxcell { private image imagevalue; private size imagesize; public override object clone() { textandimagecell c = base.clone() textandimagecell; c.imagevalue = this.imagevalue; c.imagesize = this.imagesize; homecoming c; } public image image { { if (this.owningcolumn == null || this.owningtextandimagecolumn == null) { homecoming imagevalue; } else if (this.imagevalue != null) { homecoming this.imagevalue; } else { homecoming this.owningtextandimagecolumn.image; } } set { if (this.imagevalue != value) { this.imagevalue = value; this.imagesize = value.size; padding inheritedpadding = this.inheritedstyle.padding; this.style.padding = new padding(imagesize.width, inheritedpadding.top, inheritedpadding.right, inheritedpadding.bottom); } } } protected override void paint(graphics graphics, rectangle clipbounds, rectangle cellbounds, int rowindex, datagridviewelementstates cellstate, object value, object formattedvalue, string errortext, datagridviewcellstyle cellstyle, datagridviewadvancedborderstyle advancedborderstyle, datagridviewpaintparts paintparts) { // paint base of operations content base.paint(graphics, clipbounds, cellbounds, rowindex, cellstate, value, formattedvalue, errortext, cellstyle, advancedborderstyle, paintparts); if (this.image != null) { // draw image clipped cell. system.drawing.drawing2d.graphicscontainer container = graphics.begincontainer(); graphics.setclip(cellbounds); graphics.drawimageunscaled(this.image, cellbounds.location); graphics.endcontainer(container); } } private textandimagecolumn owningtextandimagecolumn { { homecoming this.owningcolumn textandimagecolumn; } } } }

2- after that click on edit column of datagridview (on design mode) , alter column type : datagridviewtextboxcolumn column type: textandimagecolumn

3- add together image list user command 2 different images( 16 x 16) .png files.

4- , add together method display image , text value in 1 cell of datagridview:

public void imagerowdisplay() { ((textandimagecell)_tradegrid.rows[0].cells[0]).image = (image)imagelist1.images[1]; }

5- , add together info on grid rows text , image cell on button click event.

private void btninsertdata_click(object sender, eventargs e) { //code insert rows on grid. imagerowdisplay(); }

reference how insert image text in 1 cell of datagridview in c#

c# datagridview

Comments

Popular posts from this blog

c - Compilation of a code: unkown type name string -

java - Bypassing "final local variable defined in an enclosing type" -

json - Hibernate and Jackson (java.lang.IllegalStateException: Cannot call sendError() after the response has been committed) -