DataGridViewImageColumn绑定文本字段中显示

DataGridViewImageColumn绑定文本字段中显示

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

问题描述

我正在使用WindowsForm项目,而且我的表单中我有一个 DataGridView ,一个 DataGridViewImageColumn 使用图片显示行(启用/停用)的状态



我有一个 DataTable 我绑定到我的datagrid。在这个表格中,有一列是每列的状态,是一个文本字段。



如何将此列绑定到 DataGridViewImageColumn 显示正确的图像?

解决方案

只要我对DataGridView中的事情有任何疑问,请先咨询Microsoft的FAQ。





通常我在这种情况下做的是处理CellFormatting事件根据单元格中的值设置图像。



所以我将图像存储在像图像列表中,然后在CellFormatting中编写如下代码:

  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.I法师[1];
}
else
{
e.Value = imageList1.Images [2];
}
}
}
}


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.

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

解决方案

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

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

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绑定文本字段中显示图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-22 22:19