Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
373 views
in Technique[技术] by (71.8m points)

c# - 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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];
            }
        }
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...