How to display images in datagridview? WITH#

I am developing a C # desktop application using Visual Studio Express 2010.

I have a table in MySQL called Products with three fields:

ID → Product_Name → product_image

The product_Image field saves the image path on my hard drive (not the image itself)

Record Example:

0001 --- XYZ Mouse Pad ---- c: \ images \ mousepad.jpg

I wonder how to populate a datagridview that shows ID, Produt name, and - especially - the product image for each record in my SQL query.

In all the examples that I found, manual data inserts were used, but I'm looking for an example to populate a datagridview with data from an SQL query, rather than with manual insertion.

Edit:

Thank you for your help, but could not directly apply the solutions.

I already have a datagridview in my form, I do not need to create at runtime.

I need something like this (I will write a general way)

 returnMySQL = "select * from products"; while (returnMySQL) {   fill datagrid with ID, product name, product image } 
+4
source share
4 answers

Use the following code:

 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 LINK .

+7
source

You can add images as follows:

 //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)); 
+1
source

You can do this easy way.

  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; 

thats all you can do is change the height of the datagrid view and according to your requirement

0
source

Try using below when viewing your code

 <Columns> <asp:TemplateField> <ItemTemplate> <asp:Image runat="server" ImageUrl='<%# "c:/images/"+Eval("image") +"%>' Width="16" /> </ItemTemplate> </asp:TemplateField> <asp:BoundField DataField='<%#Eval("Product_Name") %>'/> </Columns> 

-2
source

Source: https://habr.com/ru/post/1483079/


All Articles