【问题标题】:How display images in datagridview? c#如何在 datagridview 中显示图像? C#
【发布时间】:2013-05-22 23:24:12
【问题描述】:

我正在使用 Visual Studio Express 2010 用 C# 开发桌面应用程序。

我在 MySQL 中有一个名为 Products 的表,其中包含 3 个字段:

ID -> Product_Name -> product_image

product_Image 字段将图像路径存储在我的硬盘中(不是图像本身)

记录的一个例子是:

0001 --- 鼠标垫 XYZ ---- c:\images\mousepad.jpg

我想知道如何填充一个datagridview,它为我的 SQL 查询中的每条记录显示ID, Produt name, and - especially - the product image

我发现的所有示例都使用了手动数据插入,但我正在寻找一个示例来使用来自 SQL 查询的数据填充 datagridview,而不是手动插入。

编辑:

感谢您的帮助,但无法直接应用解决方案。

我的表单上已经有一个datagridview,我不需要在运行时创建。

我需要这样的东西(我会写一个通用的方式)

returnMySQL = "select * from products";

while (returnMySQL)
{
    fill datagrid with ID, product name, product image
}

【问题讨论】:

  • 你是否在一个属性中拥有所有 3 个三个值?
  • 您需要将图像路径存储在database 中,并且需要将image 本身存储到项目中的文件夹中,然后应用选择查询并将datagridview 与普通查询绑定,

标签: c# database winforms datagrid datagridview


【解决方案1】:

你可以这样做

            SqlConnection conn=New   SqlConnection("SERVER=127.0.0.1;DATABASE=bdss;UID=sa;PASSWORD=1234");
            SqlDataAdapter adpt = new SqlDataAdapter("select * from products",conn);
            DataTable dt = new System.Data.DataTable();
            adpt.Fill(dt);
            int count = dt.Rows.Count;

            dataGridView1.DataSource = dt;

这就是您可以根据您的要求更改 Datagrid 视图高度的所有内容

【讨论】:

    【解决方案2】:

    您可以通过以下方式添加图片:

    //you need to perform some parsing to retrieve individual values of ID, Name and ImagePath
    string path = @"c:\images\mousepad.jpg";
    string ID = "0001";
    string Product_Name = "Mousepad XYZ";
    dataGridView1.Rows.Add(ID, Product_Name, Bitmap.FromFile(path));
    

    【讨论】:

      【解决方案3】:

      使用以下代码:

      Bitmap img;
      
      img = new Bitmap(@"c:\images\mousepad.jpg");
      
      // Create the DGV with an Image column
      
      DataGridView dgv = new DataGridView();
      
      this.Controls.Add(dgv);
      
      DataGridViewImageColumn imageCol = new DataGridViewImageColumn();
      
      dgv.Columns.Add(imageCol);
      
      // Add a row and set its value to the image
      
      dgv.Rows.Add();
      
      dgv.Rows[0].Cells[0].Value = img;
      

      参考LINK

      【讨论】:

      • 我猜这是desktop标签的winform coz
      • datagridview 而不是gridview
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-01
      相关资源
      最近更新 更多