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

编程入门 行业动态 更新时间:2024-10-27 08:29:43
本文介绍了在 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/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屋!

更多推荐

[db:关键词]

本文发布于:2023-03-29 10:26:21,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/753677.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字段   绑定   图像   文本   DataGridViewImageColumn

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!