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?
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];
}
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With