繁体   English   中英

如何用图像替换数据网格视图中的数字?

[英]How can I replace numbers in a data grid view with images?

我从数据库中收到一个分配给我的数据网格的表,但是我想用图像替换“标签”列中的数字,但是这样做有问题。 目前,我尝试使用一个数字没有成功。 列的类型为文本框,但将其更改为图像对我也不起作用。

我尝试使用CellFormating,但可以找到我感兴趣的字段,但是我不知道如何用图像替换数字。

private void DgvCriticasUser_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
    if (dgvCriticasUser.Columns[e.ColumnIndex].Name == "tag")
    {
        if (e.Value.ToString() == "1")
        {
            Bitmap tag1 = (Bitmap)Bitmap.FromFile(@"C:\Users\guill\Desktop\ProyectoIntegradoVerde-master\ProyectoEquipoVerde\ProyectoEquipoVerde\imagenes\grupo_w.png");

            DataGridViewImageCell iCell = new DataGridViewImageCell();
            iCell.Value = tag1;

            e.Value = iCell;
        }
    }
}

您可以采取的另一种方法是隐藏 “标签”列:

this.dataGridView1.Columns["YourColumnName"].Visible = false;

在所需的index (例如0)处创建并插入一个DataGridViewImageColumn

DataGridViewImageColumn imageCol = new DataGridViewImageColumn();
this.dataGridView1.Columns.Insert(0, buttonColumn);

然后在CellFormatting事件处理程序中设置image列的值,类似于您现在尝试的方法。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM