Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Showing image in a DataGridViewImageColumn binding text-field

I'm working on a WindowsForm project and in my form I have a DataGridView with a DataGridViewImageColumn which must show the status of the row (enabled/disabled) using an image.

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

How can I bind this column to the DataGridViewImageColumn showing the right image?

like image 798
davioooh Avatar asked Mar 20 '12 15:03

davioooh


1 Answers

Whenever I have questions on how to do things in a DataGridView I consult Microsoft's FAQ first.

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

Typically what I do in that situation is handle the CellFormatting event to set the image based on the value in the cell.

So I would store my images in something like an image list, then have code in CellFormatting like the following:

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];
            }
        }
    }
}
like image 66
UWSkeletor Avatar answered Nov 10 '22 16:11

UWSkeletor