Here i am having pictures of emplyees. I have added these pictures in Folder. folders is in my project directory. pictures name are same as EmployeeId like
1.jpg
2.jpg
..
..
12.jpg
OnRowDataBound i am binding the images to Image control.
HTML:
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server" OnRowDataBound="GridView1_OnDataBound" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="EmployeeID" HeaderText="EmployeeID" />
<asp:BoundField DataField="FirstName" HeaderText="FirstName" />
<asp:BoundField DataField="Country" HeaderText="Country" />
<asp:BoundField DataField="Title" HeaderText="Title" />
<asp:TemplateField HeaderText="Image">
<ItemTemplate>
<asp:Image ID="imgEmployeeImage" runat="server" Width="80" Height="80" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
</form>
C#:
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
this.GetData();
}
}
private void GetData()
{
string constr = ConfigurationManager.ConnectionStrings["ConString"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand("SELECT EmployeeID,FirstName,LastName,Country,Title FROM Employees", con))
{
using (SqlDataAdapter da = new SqlDataAdapter(cmd))
{
DataSet ds = new DataSet();
da.Fill(ds);
if (ds.Tables[0].Rows.Count > 0)
{
this.GridView1.DataSource = ds;
this.GridView1.DataBind();
}
}
}
}
}
protected void GridView1_OnDataBound(object sender, GridViewRowEventArgs e)
{
string id;
if (e.Row.RowType == DataControlRowType.DataRow)
{
id = e.Row.Cells[0].Text;
(e.Row.Cells[4].FindControl("imgEmployeeImage") as Image).ImageUrl = "photos/" + id + ".jpg";
}
}
This is my Project directory:
Output:
Thank You.