在DataGridViewImageColumn约束力的文本字段显示图像 [英] Showing image in a DataGridViewImageColumn binding text-field

查看:159
本文介绍了在DataGridViewImageColumn约束力的文本字段显示图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作的一个WindowsForm项目,并在我的形式我有一个的DataGridView DataGridViewImageColumn 必须显示状态的行中的 使用图像(启用/禁用)。

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.

我如何可以将绑定该列的 DataGridViewImageColumn 显示正确的形象?

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

推荐答案

每当我对如何做的事情在一个DataGridView我咨询微软的常见问题解答第一个问题。

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

<一个href="http://www.windowsclient.net/Samples/Go%20To%20Market/DataGridView/DataGridView%20FAQ.doc">http://www.windowsclient.net/Samples/Go%20To%20Market/DataGridView/DataGridView%20FAQ.doc

通常我做什么,在这种情况下是处理CellFormatting事件图像基于单元格中的值来设置。

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

所以,我会存储我的图片中类似的图像列表,然后有code在CellFormatting这样的:

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

这篇关于在DataGridViewImageColumn约束力的文本字段显示图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆