Click here to Skip to main content
15,919,423 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
hi frnds,

How to display image in Gridview Based on Text displayed from Database (From Images Folder)

am working on asp.net C# and Sqlserver 2005.

presently am working on Gridview.
In Solution explorer i have images folder

I have a database with TableName(TravelDetails) with fields as
TrainCode,Train_Desc,Sch_Time,From,To

My record will look like this in Gridview as per database columns as above.

T6932 | Manmad Express | 1100 | Hyderabad | Mumbai

So, Instead of Manmad Express text I need to display logo of ManmadExpress. Which is in Images Folder.

So, we need to write code in RowDataBound as

C#
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) {
        if(e.Row.RowType == DataControlRowType.DataRow)
        {
            
        }



Please can you help me how to do this?

Thanks.
Posted

Hello Hyderabadi,

First you need to store the path of the images in the same Sql Table. Then


XML
<asp:TemplateField HeaderText="Train">
                                              <ItemTemplate>
                                                  <asp:Image ID="trainImage" Width="60px" Height="50px" runat="server" ImageUrl='<%#Eval("ImageURL") %>' />
                                              </ItemTemplate>
                                          </asp:TemplateField>



No need of using GridView1_RowDataBound.
 
Share this answer
 
v2
Comments
HYD Webdeveloper 10-Feb-14 2:23am    
Sir, I am Just Retrieving data using sql query in code behind, am not using Template fields in gridview.PLEASE HELP.
Prasad Avunoori 10-Feb-14 2:35am    
You need add one more column to your Table which stores the path of the specific images.
HYD Webdeveloper 10-Feb-14 2:43am    
Ok sir, I have added as columnname as Images in Table.. Now how to give the path
Prasad Avunoori 10-Feb-14 4:13am    
1. Update that field with actual location of the image
2. Modifie your select query as follow
Select TrainCode,Train_Desc,Sch_Time,From,Tom,Images as ImageURL From <TableName>.
try like this..

C#
<asp:gridview id="gvTraininfo" runat="server" xmlns:asp="#unknown">
    AutoGenerateColumns="False"  
    GridLines="None"  
    AllowPaging="true"  
    CssClass="mGrid"  
    AlternatingRowStyle-CssClass="alt">  
    <columns>  
        <asp:boundfield datafield="TrainCode" headertext="TrainCode" />  
        <asp:boundfield datafield="Train_Desc" headertext="Train_Desc" />  
        <asp:boundfield datafield="Sch_Time" headertext="Sch_Time" /> 
        <asp:templatefield headertext="Image" itemstyle-horizontalalign="Center">
        <itemtemplate>
            <asp:imagebutton id="ImageButton1" runat="server">
            ImageUrl='<%# Eval("fldImageUrl")%>' Width="50px"
            Height="50px" Style="cursor: pointer"
          />
        </asp:imagebutton></itemtemplate>
    </asp:templatefield>  
      
    </columns>  
</asp:gridview> 


and use SQL Select Statement like this

SQL
Select TrainCode,Train_Desc,Train_Desc,From,To,'c:/images/'+'logo.jpg' as fldImageUrl from table
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900