Change image in image button column in datagridview

It’s just interesting if someone managed to find a way to datagridview image in the template field, which displays different images in different rows depending on some data element in the row. Our application should display a series of commissions charged from the column of the account, which will be either less than 500, 500-1000, or 1000. Some of these fees are charged at full rate, others at a partial rate, and some of them are not charged. The user would like to see a simple three-state graphic that would convey simple info: full payment, partial payment or no charge. It sounds easy enough, but I have not yet been able to find a way to do this. Every time I try to change the individual cell graphics, it seems to want to change the graphics for all rows.

How to get around this problem?

Thanks,

+2
source share
2 answers

You can do it with

<TemplateField> <ItemTemplate> <asp:ImageButton runat="server" ImageUrl='<%# GetImage(Eval("check") as string) %>' /> </ItemTemplate> </TemplateField> 

Where GetImage() is the method in your code:

 protected string GetImage(string check) { if ((check == "2") || (check == "null") || (check == "3") || (check == "-1")) { return "../Interbacs/images/error.gif"; } else if (check == "1") { return "../Interbacs/images/ok.png" } return "what?"; } 

This can probably be made a little “prettier,” but I hope you get the idea.

0
source

U can change the image in CellPainting datagridview.

 eg: private void datagridview_CellPainting(object sender, DataGridViewCellPaintingEventArgs e) { if (e.ColumnIndex == 1 && e.RowIndex >= 0) { if (Convert.ToInt32 (datagridview.Rows[e.RowIndex].Cells["columnName"].Value)<500) { e.Paint(e.CellBounds, DataGridViewPaintParts.All); e.Graphics.DrawImage('Give Image path here', e.CellBounds.Left + 5, e.CellBounds.Top + 5); } } } 

OR check this out http://www.codeproject.com/KB/grid/DGV_ImageButtonCell.aspx/

+1
source

Source: https://habr.com/ru/post/892459/


All Articles