无法在datagridview winforms中更改图像 [英] cannot change image in datagridview winforms

查看:107
本文介绍了无法在datagridview winforms中更改图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我有以下代码:


我需要更改列的图像,如果另一列中指定的路径存在。

  dataGridView1.DataSource = table; 


DataGridViewButtonColumn buttonCol = new DataGridViewButtonColumn();
buttonCol.HeaderText =;
buttonCol.Name =BrowseButton;
buttonCol.Text =...;
buttonCol.UseColumnTextForButtonValue = true;
dataGridView1.Columns.Add(buttonCol);

DataGridViewImageColumn imgCol = new DataGridViewImageColumn();
imgCol.HeaderText =状态;
imgCol.Name =StatusImage;
imgCol.Image = null;
dataGridView1.Columns.Add(imgCol);
dataGridView1.Columns [StatusImage]。DisplayIndex = 4;

foreach(DataGridViewRow myRow in dataGridView1.Rows)
{
string overRiddenDirPath = myRow.Cells [Overridden Dir] Value.ToString();
string preConfiguredPath = myRow.Cells [PreConfigured Dir]。Value.ToString();
string path = overRiddenDirPath;
if(overRiddenDirPath ==)
{
path = preConfiguredPath;
}

DataGridViewImageCell cell = myRow.Cells [StatusImage]作为DataGridViewImageCell;

//如果目录不存在
if(!Directory.Exists(path))
{
cell.Value = Image.FromFile(@ Chrysanthemum.jpg);
}
else
{
cell.Value = Image.FromFile(@Jellyfish.jpg);
}
}

没有图像显示:
图像的路径很好,因为如果我这样写:

  DataGridViewImageColumn imgCol = new DataGridViewImageColumn(); 
imgCol.HeaderText =状态;
imgCol.Name =StatusImage;
imgCol.Image = Image.FromFile(@Chrysanthemum.jpg);
dataGridView1.Columns.Add(imgCol);
dataGridView1.Columns [StatusImage]。DisplayIndex = 4;

它显示,但不会在条件上更改。



另外,有没有更好的方法来添加图像到datagridview单元格。



任何帮助是赞赏。谢谢

解决方案

您可以将代码放在 CellFormatting 事件中。

  void dataGridView1_CellFormatting(object sender,DataGridViewCellFormattingEventArgs e)
{
if(dataGridView1.Columns [ ColumnIndex] .Name ==StatusImage)
{
//你的代码将在这里 - 下面只是我用来测试
的代码e.Value = Image.FromFile(@ C:\Pictures\TestImage.jpg);
}
}

要注意的一个重要事情是你设置e






这是一个例子中的代码,我试图在哪里访问另一列有条件地更改所选图像。无论图像具有哪个显示索引,它都可以完美地工作。

  void dataGridView1_CellFormatting(object sender,DataGridViewCellFormattingEventArgs e)
{
if(!dataGridView1.Rows [e.RowIndex] .IsNewRow)
{
if(dataGridView1.Columns [e.ColumnIndex] .Name ==StatusImage)
{
if(((int)dataGridView1.Rows [e.RowIndex] .Cells [ValueTwo]。Value)== 5)
{
e.Value = Image.FromFile(@ C:\Pictures\TestImage1.jpg);
}
else
{
e.Value = Image.FromFile(@C:\Pictures\TestImage2.jpg);
}
}
}
}

该示例我有一个整数值的列,但它应该以类似的方式适用于文本。


i need to change the image of the column if a path specified in another column exists.

i have the following code:

dataGridView1.DataSource = table;


        DataGridViewButtonColumn buttonCol = new DataGridViewButtonColumn();
        buttonCol.HeaderText = "";
        buttonCol.Name = "BrowseButton";
        buttonCol.Text = "...";
        buttonCol.UseColumnTextForButtonValue = true; 
        dataGridView1.Columns.Add(buttonCol);

        DataGridViewImageColumn imgCol = new DataGridViewImageColumn();
        imgCol.HeaderText = "Status";
        imgCol.Name = "StatusImage";
        imgCol.Image = null;
        dataGridView1.Columns.Add(imgCol);
        dataGridView1.Columns["StatusImage"].DisplayIndex = 4;

 foreach (DataGridViewRow myRow in dataGridView1.Rows)
        {
            string overRiddenDirPath = myRow.Cells["Overridden Dir"].Value.ToString();
            string preConfiguredPath = myRow.Cells["PreConfigured Dir"].Value.ToString();
            string path = overRiddenDirPath;
            if (overRiddenDirPath == "")
            {
                path = preConfiguredPath;
            }

            DataGridViewImageCell cell = myRow.Cells["StatusImage"] as DataGridViewImageCell;

            // If the directory doesn't exist
            if (!Directory.Exists(path))
            {
                cell.Value = Image.FromFile(@"Chrysanthemum.jpg");
            }
            else
            {
                cell.Value = Image.FromFile(@"Jellyfish.jpg");                   
            }
        }

no image is getting displayed: the path of the image is fine because if i put it like this :

  DataGridViewImageColumn imgCol = new DataGridViewImageColumn();
        imgCol.HeaderText = "Status";
        imgCol.Name = "StatusImage";
        imgCol.Image = Image.FromFile(@"Chrysanthemum.jpg");
        dataGridView1.Columns.Add(imgCol);
        dataGridView1.Columns["StatusImage"].DisplayIndex = 4;

it gets displayed but it does not change on the condition.

Also, is there a better way to add images to a datagridview cells.

Any help is appreciated. thanks

解决方案

You can put your code in the CellFormatting event instead.

void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{            
    if (dataGridView1.Columns[e.ColumnIndex].Name == "StatusImage")
    {
        // Your code would go here - below is just the code I used to test
        e.Value = Image.FromFile(@"C:\Pictures\TestImage.jpg");
    }
}

The one important thing to note is that you set e.Value rather than cell.Value here.


Here is the code from an example I tried where I access the value of another column to conditionally change the selected image. This works perfectly regardless of which display index the image has.

void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
    if (!dataGridView1.Rows[e.RowIndex].IsNewRow)
    {
        if (dataGridView1.Columns[e.ColumnIndex].Name == "StatusImage")
        {
            if (((int)dataGridView1.Rows[e.RowIndex].Cells["ValueTwo"].Value) == 5)
            {
                e.Value = Image.FromFile(@"C:\Pictures\TestImage1.jpg");
            }
            else
            {
                e.Value = Image.FromFile(@"C:\Pictures\TestImage2.jpg");
            }
        }
    }
}

In the example I have a column with integer values within it, but it should work for text in a similar fashion.

这篇关于无法在datagridview winforms中更改图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆