数据绑定后如何将图像设置为数据网格视图?

时间:2011-05-03 23:45:35

标签: c# image datagridview cell formatting

我在数据绑定后向DGV单元添加图像时遇到问题。

这是我的代码:

                DataTable tab = conn.searchData(searchTmp);
              bindingSource1.DataSource = tab;
              DGV.AllowUserToAddRows = false;

              DGV.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCellsExceptHeader);                
              //dont show this colls
              DGV.Columns["zId"].Visible = false;
              DGV.Columns["tId"].Visible = false;
              DGV.Columns["tId2"].Visible = false;

              DataGridViewImageColumn co = new DataGridViewImageColumn();
              System.Reflection.Assembly thisExe;
              thisExe = System.Reflection.Assembly.GetExecutingAssembly();
              System.IO.Stream file = thisExe.GetManifestResourceStream("MyApp.Resources.Tx1[k].gif");
              System.IO.Stream file2 = thisExe.GetManifestResourceStream("MyApp.Resources.Tx2[k].gif");

//和其他所有列 - 第一个灰色行

              Bitmap bmp = new Bitmap(file);
              co.Image = bmp;

              DataGridViewImageColumn img = new DataGridViewImageColumn();
              Image image = bmp;
              img.Image = image;
              DGV.Columns.Add(img);
              img.HeaderText = "Image";
              img.Name = "img";

数据表是数据库的结果, 在第一个coll我有TeX表达式 - 我想用“MimeTex.dll”为这个表达式生成图像,我知道怎么做但我不知道如何用图像替换这个TeX表达式,
在屏幕上是我的原始DGV,没有图像。

在最后六行中我有一部分代码用于添加新列,因为我测试并尝试如何将第一行列文本(标题行)替换为来自apps资源的静态图像而没有成功...

有什么想法吗? TIA 屏幕: http://www.freeimagehosting.net/image.php?66fe2964fe.jpg

1 个答案:

答案 0 :(得分:12)

  DataGridViewImageColumn ic= new DataGridViewImageColumn();
  ic.HeaderText = "Img";
  ic.Image = null;
  ic.Name = "cImg";
  ic.Width = 100;
  DGV.Columns.Add(ic);


  foreach (DataGridViewRow row in DGV.Rows)
  {
    DataGridViewImageCell cell = row.Cells[1] as DataGridViewImageCell;
    cell.Value = (System.Drawing.Image)Properties.Resources.Icon_delete;
  }