如何在DataGridViewImageColumn中添加图像

本文关键字:添加 图像 DataGridViewImageColumn | 更新日期: 2023-09-27 18:20:28

我有一个字段DataGridViewImageColumn,根据条件,为字段的每一行添加不同的图像。有人知道我如何在Windows窗体中做到这一点吗?

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

如何在DataGridViewImageColumn中添加图像

  1. 将您的图像添加到properties文件夹下的Resources.resx中。(例如图片1.jpeg)
  2. DataGridView中添加DataGridViewImageColumn
  3. 以这种方式添加图像:

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

使用以下代码:

        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;
        }

使用此代码

 protected void gridView1_RowDataBound(Object sender, GridViewRowEventArgs args)
    {
     if(args.Row.RowType == DataControlRowType.DataRow)
     {
      Image img = (Image) e.Row.FindControl("Image1");
      img.ImageUrl = setImageURLHere;
     }
    }
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);
}

在项目中添加图像作为资源,并应用下面的代码

DataGridViewImageColumn btnDel = new DataGridViewImageColumn();
btnDel.Name = "DelCourrier";
btnDel.HeaderText = "";
btnDel.Image = Properties.Resources.delete;// delete is the name of the image added as ressource
dataGridView1.Columns.Add(btnDel);