Reboot
Reboot

Reputation: 83

How display images in datagridview? c#

I am developing an application in C # for desktop using Visual Studio Express 2010.

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

ID -> Product_Name -> product_image

The field product_Image stores the image path in my hard drive (not the image itself)

An example of a record would be:

0001 --- Mousepad XYZ ---- c:\images\mousepad.jpg

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

All the examples I found were used manual data inserts, but I am looking for an example to fill the datagridview with data from a SQL query, not a manual insertion.

Edit:

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

I already have a datagridview on my form, I have no need to create in runtime.

I need something like that (I'll write a generic way)

returnMySQL = "select * from products";

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

Upvotes: 8

Views: 57558

Answers (3)

You can Doing this simple 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 change Datagrid view height and with according your requirment

Upvotes: -1

Shaharyar
Shaharyar

Reputation: 12459

You can add images with the following way:

//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));

Upvotes: 4

Freelancer
Freelancer

Reputation: 9074

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

Referance LINK .

Upvotes: 11

Related Questions