Showing image in a DataGridViewImageColumn binding text-field

后端 未结 1 2018
梦如初夏
梦如初夏 2021-01-12 16:13

I\'m working on a WindowsForm project and in my form I have a DataGridView with a DataGridViewImageColumn which must show the status

相关标签:
1条回答
  • 2021-01-12 16:30

    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];
                }
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题