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

Image Column in Datagridview in C#


C# C# 
Ratings:
0 Comment(s)
 12667  View(s)
Rate this:

Image Column in Datagridview in C#

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.

1.  System.Net.HttpWebRequest _myRequest;

2.  System.Net.HttpWebResponse _myResponse;

3.  System.Drawing.Bitmap _img;

4.  try

5.  {

6.  _myRequest = (System.Net.HttpWebRequest)System.Net.WebRequest.Create("http://www.mindstick.com/Images/logo.jpg");

7.  _myRequest.Method = "GET";

8.  _myResponse = (System.Net.HttpWebResponse)_myRequest.GetResponse();

9.  _img = new System.Drawing.Bitmap(_myResponse.GetResponseStream());

10. _myResponse.Close();

11. dgvImage.Rows.Add("Mindstick", _ImageToByte(_img));

12. dgvImage.Rows[0].Height = dgvImage.Rows[0].Height + 40;

13. }

14. catch (Exception ex)

15. {

16. MessageBox.Show("Some Problem Occurred");

17. }

_ImageToByte Method:-

1.  public static byte[] _ImageToByte(Image img)

2.  {

3.  ImageConverter converter = new ImageConverter();

4.  return (byte[])converter.ConvertTo(img, typeof(byte[]));

5.  }

 

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.


Don't want to miss updates? Please click the below button!

Follow MindStick