我如何显示图像与数据从datatable到datagridview

时间:2018-07-10 09:07:33

标签: c#

我想用图像和数据表中的数据加载datagridview,其中包含我的表数据,图像名称存储在image_item列中

我在这里创建了图像列实例

DataGridViewImageColumn imgCol = new DataGridViewImageColumn();
imgCol.HeaderText = "Image";
imgCol.Name = "image";
dgv_items.Columns.Add(imgCol);  

在这里我得到了我的图像目录路径

string appPath = Path.GetDirectoryName(Application.ExecutablePath) +@"\itemImages\";
if (Directory.Exists(appPath) == false)
{
   Directory.CreateDirectory(appPath);
}

//在这里,我遍历数据表的每一行

foreach (DataGridViewRow row in dgv_items.Rows)
{
     if (row.Cells["item_image"].Value.ToString().Equals(""))
     {
        //if my DataTable image Column have null value show default image
        row.Cells["image"].Value = Properties.Resources.download;
     }
     else
     {
       // Here i want to display image on datagridview cell
        row.Cells["image"].Value = Bitmap.FromFile(Path.Combine(appPath, 
        row.Cells["item_image"].Value.ToString()));
      }
 }

0 个答案:

没有答案