Images in gridview control - c# example image in gridview
In we need to show images in gridview. To show image in gridview we have to use TemplateColumn in gridview and Image web control inside it. 2.0 have ImageField that we can use to view image in gridview.

Sample code to show image in gridview [ c#]

<html xmlns="">
<head runat="server">
    <form id="form1" runat="server">
    <asp:GridView ID="GridView1" Runat="server" AutoGenerateColumns="False" 
  BorderWidth="1px" CellPadding="3" BorderStyle="None" 
    <PagerStyle HorizontalAlign="Left"></PagerStyle>
    <HeaderStyle Font-Bold="True"></HeaderStyle>
        <asp:BoundField HeaderText="Image No." DataField="ImageID">
            <ItemStyle HorizontalAlign="Center" 
        <asp:ImageField DataImageUrlField="ImagePath"></asp:ImageField>

Below is C# code to show image in gridview

using System;
using System.Data;

namespace aspnetcontrol
    public partial class GridviewImages : System.Web.UI.Page
        protected void Page_Load(object sender, EventArgs e) 
            GridView1.DataSource = BindImageData();

       public DataTable BindImageData()     
            DataTable dt = new DataTable();
            dt.Columns.Add(new DataColumn("ImageID", typeof(int)));
            dt.Columns.Add(new DataColumn("ImagePath", typeof(string)));
            // Create the four records
            DataRow dr = dt.NewRow();
            dr["ImageID"] = 1;
            dr["ImagePath"] = ResolveUrl("~/Images/image1.jpg");
            dr = dt.NewRow();
            dr["ImageID"] = 2;
            dr["ImagePath"] = ResolveUrl("~/Images/image2.jpg");
            dr = dt.NewRow();
            return dt;

In above code we have used TemplateCloumn of gridview and ImageField control inside it. Here we can set any datasource that contains image and bind it to gridview. Here in this example I have created sample datatable with image column and bind it to gridview to show image in gridview.

1 comment :

  1. I followed this tutorial by creating a table in the database where I stored the data of images in the stored table. After finishing with the code and running it I found the images being displayed successfully. Now thinking to modify this application to have more impressive output.