Display image in DataGridViewImageColumn binding text box

I am working on a WindowsForm project, and in my form I have a DataGridView with a DataGridViewImageColumn that should show the status of the row (on / off) using the image.

I have a DataTable that I bind to my datagrid. This table has a column that is the status of each row and is a text field.

How to associate this column with a DataGridViewImageColumn with the image of the correct image?

+6
source share
1 answer

Whenever I have questions about how to do things in a DataGridView, I first turn to the Microsoft FAQ.

http://www.windowsclient.net/Samples/Go%20To%20Market/DataGridView/DataGridView%20FAQ.doc

In this situation, I usually do the processing of the CellFormatting event to set the image based on the value in the cell.

So, I would save my images as a list of images, and then the code in CellFormatting, as shown below:

 private void dgv_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e) { if (dgv.Columns[e.ColumnIndex].Name == "status") { if (e.Value != null) { if (e.Value.ToString() == "1") { e.Value = imageList1.Images[1]; } else { e.Value = imageList1.Images[2]; } } } } 
+13
source

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


All Articles