Free Jobs

Dot Net Forums: ASPNET (77) C#.NET (39) VB.NET (8) AJAX (21) Silver Light (1) WCF (10) WPF (1) WWF (1) .NET 3.5 (3) SQL SERVER (38) Other (3)

Topic: How to add Image to gridview in ASPNET by adding new column to dataset

To get the Posted Questions details daily in your mail Click Here

Share this Post on Facebook/Twitter/Orkut Bookmark and Share

User: avinash222 Post Date: 15 Feb 2011 08:20

In this post we are going to discuss How to add Image to gridview in ASPNET
by adding new column to dataset we will be using C# language for

Main Content:
Add the below syntax of gridview to .aspx page

<asp:GridView ID="GridViewPlayers" runat="server" AutoGenerateColumns="False"> <Columns> <asp:ImageField DataImageUrlField="ImageURL" ControlStyle-Height="50" ControlStyle-Width="50" HeaderText="Image"> </asp:ImageField> <asp:BoundField HeaderText="Employee Name" DataField="EmployeeName"> <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle"> </ItemStyle> </asp:BoundField> </Columns> </asp:GridView>

Assuming that the Images of the employees are stored in the "~/images/Employees/" folder of the website
and the image name is same as employee name.

Add a new column named ImageURL to the dataset of employee
as below.
dsEmployee.Tables[1].Columns.Add(new DataColumn("ImageURL",typeof(string)));
string EmployeeName = string.Empty;

for (int i = 0; i < dsEmployee.Tables[1].Rows.Count; i++)
EmployeeName = dsEmployee.Tables[1].Rows[i]["EmployeeName"].ToString();
dsEmployee.Tables[1].Rows[i]["ImageURL"] = "~/images/Employees/" + EmployeeName.Replace(" ","_") + ".jpg";
GridViewEmployee.DataSource = dsEmployee.Tables[1];

Now just databind the Gridview.

In this way we can add Image to gridview if we have image for
each data in grid view

About Writer:
Hi All This is avinash from Gurgaon

Writer's Link:

Writer's Email:

Post Comment

Similar Articles Top ArticlesLatest Articles Other Articles by this Author Copyright © 2009-2011 | All Rights Reserved
All content of this website is protected by copyright laws and should not be reproduced in any form or by any means without written permission
(Best viewed in IE 8.0+ or Firefox 3.0+ at resolution of 1024 * 768 or higher)