在 DataGridViewImageColumn 绑定文本字段中显示图像 [英] Showing image in a DataGridViewImageColumn binding text-field

查看:31
本文介绍了在 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.

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

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 中执行操作的问题时,我都会先咨询 Microsoft 的常见问题解答.

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

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

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

所以我会将我的图像存储在类似图像列表的东西中,然后在 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天全站免登陆