[WinForm]- 设置DataGridView单元格内根据不同值显示图片

本文介绍如何在DataGridView中根据数据值动态显示不同的图片,并提供了一个具体的实现示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

首先设置要显示图片的列

  DataGridViewImageColumn status = new DataGridViewImageColumn();
            status.DisplayIndex = 0;
            status.HeaderText = "Status";
            status.DataPropertyName = "IsPass";
            status.ImageLayout = DataGridViewImageCellLayout.Zoom;
            dgvTestSteps.Columns.Insert(0, status);

添加DataGridView事件CellFormatting

 dgvTestSteps.CellFormatting += new DataGridViewCellFormattingEventHandler(dgvTestSteps_CellFormatting);

在事件内添加根据数据值显示不同图片的方法,判断列的地方有很多条件可以进行判断,但是通过Index感觉是最不可取的,通过HeadName也会有上下对不上的情况。

  void dgvTestSteps_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            if (dgvTestSteps.Columns[e.ColumnIndex].HeaderText.Equals("Status"))
            {
                if (e.Value == null)
                {
                    return;
                }              
                StepType type = (StepType)Enum.Parse(typeof(StepType), e.Value.ToString(), true);
                switch (type)
                {
                    case StepType.Success:
                        e.Value = Resources.check;
                        break;
                    case StepType.Fail:
                        e.Value = Resources.close_delete;
                        break;
                    case StepType.Block:
                        e.Value = Resources.attention;
                        break;
                    case StepType.NoRun:
                        e.Value = Resources.green_ball;
                        break;
                }
            }
        }

转载于:https://www.cnblogs.com/slow/p/3380527.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值