在DataGridViewImageColumn绑定文本字段中显示图像

dav*_*ooh 7 .net c# datagridview winforms

我工作的一个WindowsForm项目,并在我的表单我有一个DataGridViewDataGridViewImageColumn它必须显示状态的行使用图像(启用/禁用).

我有一个DataTable绑定到我的数据网格的东西.在此表中,有一列是每行的状态,是一个文本字段.

如何将此列绑定到DataGridViewImageColumn显示正确的图像?

UWS*_*tor 14

每当我对如何在DataGridView中执行操作有疑问时,请先咨询Microsoft的FAQ.

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

通常我在这种情况下做的是处理CellFormatting事件以根据单元格中的值设置图像.

所以我将我的图像存储在像图像列表之类的东西中,然后在CellFormatting中使用如下代码:

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];
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)