C# 如何在 DataGridViewImageColumn 中添加图像?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17365965/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 09:18:12  来源:igfitidea点击:

How do I add an image in my DataGridViewImageColumn?

c#imagedatagridview

提问by Flávio Costa

I have a field DataGridViewImageColumn, and for each line of the field, depending on a condition, I add a different image. Anyone know how I can do this in Windows Forms?

我有一个 field DataGridViewImageColumn,对于该字段的每一行,根据条件,我添加了不同的图像。任何人都知道如何在 Windows 窗体中执行此操作?

if (dgvAndon.Rows[e.RowIndex].Cells["urgencyOrder"].ToString() == "1")
{
   //Here I want to add the image in the image property field DataGridViewImageColumn.
}

采纳答案by Manoj

  1. Add your image in Resources.resx under properties folder. (ex. Picture1.jpeg)
  2. Add a DataGridViewImageColumnin your DataGridView
  3. Add image this way:

    for (int row = 0; row <= [YourDataGridViewName].Rows.Count - 1; row++)
    {
        ((DataGridViewImageCell)gvFiles.Rows[row].Cells[1]).Value = Properties.Resources.Picture1
    }
    
  1. 在属性文件夹下的 Resources.resx 中添加您的图像。(例如图片1.jpeg)
  2. DataGridViewImageColumn在你的添加一个DataGridView
  3. 以这种方式添加图像:

    for (int row = 0; row <= [YourDataGridViewName].Rows.Count - 1; row++)
    {
        ((DataGridViewImageCell)gvFiles.Rows[row].Cells[1]).Value = Properties.Resources.Picture1
    }
    

回答by ketan italiya

use this code

使用此代码

 protected void gridView1_RowDataBound(Object sender, GridViewRowEventArgs args)
    {
     if(args.Row.RowType == DataControlRowType.DataRow)
     {
      Image img = (Image) e.Row.FindControl("Image1");
      img.ImageUrl = setImageURLHere;
     }
    }

回答by Koorosh

use this code:

使用此代码:

        DataGridViewImageColumn iconColumn = new DataGridViewImageColumn();
        iconColumn.Name = "AirplaneImage";
        iconColumn.HeaderText = "Airplane Image";
        dataGridView1.Columns.Insert(5, iconColumn);

        for (int row = 0; row < dataGridView1.Rows.Count - 1; row++)
        {
            Bitmap bmp = new Bitmap(Application.StartupPath + "\Data\AirPlaneData\" + dt.Rows[row][4]);
            ((DataGridViewImageCell)dataGridView1.Rows[row].Cells[5]).Value = bmp;
        }

回答by Cistelican Catalin Videographe

string FileName = null;

OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.RestoreDirectory = true;

openFileDialog.Filter = "All picture files (*.BMP;*.JPG;*.GIF)|*.BMP;*.JPG;*.GIF";

if (openFileDialog.ShowDialog() == DialogResult.OK)
{
    FileName = openFileDialog.FileName;
    //pictureBox2.Image = Image.FromFile(FileName);
}