articles

Home / DeveloperSection / Articles / Image Column in Datagridview in C#

Image Column in Datagridview in C#

Vijay Shukla20506 07-Jun-2013

In this article I am trying to explain how to show image in datagridview image column.

Datagridview image column is use for show image in the datagridview; we have two choice for creating the image column in datagridview.

1.     Add Image Column in datagridview property.

    a.       Click on smart tag.

    b.      Click on Add column.

    c.       Give the name of column.

   d.      Select the Column type (DataGridViewImageColumn).

   e.      And then click on to add button.

Image Column in Datagridview in C#

2.       Add Image Column in datagridview dynamically.

DataGridViewImageColumn imageCol = new DataGridViewImageColumn();
dgvImage.Columns.Add(imageCol); //(dgvImage is datagridview name)

 

Note:- Above line of code create a DataGridViewImageColumn with imageCol


name and then add this column in datagridview.

 

Now I will take an example with 1 option.

 

After adding the imageColumn I will take a button with name Show, When I click


on the Show button then image will show on the datagridview ImageColumn.

 

Below image is show the demo UI:-

Image Column in Datagridview in C#

        I.     Now hit to double on the Show button for getting the show button’s click event

      II.    Write the code on Show button click event for set image in the datagridview image column.

  System.Net.HttpWebRequest _myRequest;

System.Net.HttpWebResponse _myResponse;
System.Drawing.Bitmap _img;
try
{
_myRequest = (System.Net.HttpWebRequest)System.Net.WebRequest.Create("http://www.mindstick.com/Images/logo.jpg");
_myRequest.Method = "GET";
_myResponse = (System.Net.HttpWebResponse)_myRequest.GetResponse();
_img = new System.Drawing.Bitmap(_myResponse.GetResponseStream());
_myResponse.Close();
dgvImage.Rows.Add("Mindstick", _ImageToByte(_img));
dgvImage.Rows[0].Height = dgvImage.Rows[0].Height + 40;
}
catch (Exception ex)
{
MessageBox.Show("Some Problem Occurred");
}
_ImageToByte Method:-
  public static byte[] _ImageToByte(Image img)

{
ImageConverter converter = new ImageConverter();
return (byte[])converter.ConvertTo(img, typeof(byte[]));
}

 Code Description:-

1.       Create a variable with _myRequest name as HttpWebRequest.

2.       Create a variable with _ myResponse name as HttpWebResponse.

3.       Create a variable with _img name as Bitmap.

4.       Start try block

5.       try block start {.

6.       Initialize the _myRequest variable value.

7.       Give the Method type “GET”

8.       Get the response in _myResponse.

9.       Read the image from GetResponseStream and save in _img variable.

10.   Close the Response object.

11.   Add a row in datagridview rows.

12.   Increase the height of datagridview rows.

13.   Close the try block }

Code Description _ImageToByte Method:-

1.       Create a _ImageToByte(Image img) method with single parameter and its return type is byte[] and access specifies is public.

2.       Start the method boundary with {

3.       Create a ImageConverter reference object with converter name.

4.       This line return the converted image.


c# c# 
Updated 04-Mar-2020

Leave Comment

Comments

Liked By