468,774 Members | 2,626 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,774 developers. It's quick & easy.

displaying images in gridview

Hello Everyone,

I want to display an image in my gridview. The only thing is my datatabse table is returning true if there is an image and false if there is no image
and I am binding my gridview with dataset. This dataset contains a colun called hasAttachments as true or false.



Is it possible to display image in the image field? This image does not exsist in the database. I have image in the image folder in my application code.

view plaincopy to clipboardprint?
Expand|Select|Wrap|Line Numbers
  1. private void BindGridVIew()   
  2.        {   
  3.            try  
  4.            {   
  5.                DataSet ds = getscrollData();   
  6.  
  7.                grdScrolls.DataSource = ds;   
  8.                grdScrolls.DataBind();   
  9.            }   
  10.            catch (Exception ex)   
  11.            {   
  12.  
  13.  
  14.            }   
  15.  
  16.        }   
Expand|Select|Wrap|Line Numbers
  1.  
  2. <asp:GridView ID="grdScrolls" runat="server" Width="100%"    
  3.                onrowcommand="grdScrolls_RowCommand" onrowcreated="grdScrolls_RowCreated"    
  4.                AutoGenerateColumns="false"    
  5.                EnableViewState="true" onrowdatabound="grdScrolls_RowDataBound"    
  6.                AllowPaging="true" onpageindexchanging="grdScrolls_PageIndexChanging" PagerSettings-Mode="NumericFirstLast" PageSize="25" BorderStyle="None" >   
  7.  
  8.              <Columns>   
  9.               <asp:ImageField DataImageUrlField="HasAttachment"></asp:ImageField>   
  10.  </Columns>  
  11.            </asp:GridView>  
  12.  
Expand|Select|Wrap|Line Numbers
  1.  private void BindGridVIew()
  2.         {
  3.             try
  4.             {
  5.                 DataSet ds = getscrollData();
  6.  
  7.                 grdScrolls.DataSource = ds;
  8.                 grdScrolls.DataBind();
  9.             }
  10.             catch (Exception ex)
  11.             {
  12.  
  13.  
  14.             }
  15.  
  16.  
  17.         }
Expand|Select|Wrap|Line Numbers
  1.  <asp:GridView ID="grdScrolls" runat="server" Width="100%" 
  2.                 onrowcommand="grdScrolls_RowCommand" onrowcreated="grdScrolls_RowCreated" 
  3.                 AutoGenerateColumns="false" 
  4.                 EnableViewState="true" onrowdatabound="grdScrolls_RowDataBound" 
  5.                 AllowPaging="true" onpageindexchanging="grdScrolls_PageIndexChanging" PagerSettings-Mode="NumericFirstLast" PageSize="25" BorderStyle="None" >
  6.  
  7.               <Columns>
  8.                <asp:ImageField DataImageUrlField="HasAttachment"></asp:ImageField>
  9.   </Columns>
  10.             </asp:GridView>

Any help will be greatly appreciated.
Sep 17 '09 #1
3 2867
ssnaik84
149 100+
Expand|Select|Wrap|Line Numbers
  1. <asp:GridView ID="grdScrolls" runat="server" AutoGenerateColumns="False">
  2.                         <Columns>
  3.                         <asp:TemplateField HeaderText="Image" HeaderStyle-Wrap="false">
  4.                                 <ItemTemplate>                               
  5.                                  <%#GetImageUrl()%>
  6.                                  </ItemTemplate>
  7.                                 <HeaderStyle Wrap="False" />
  8.                             </asp:TemplateField>  ..... 
implement protected string GetImageUrl()
which will return image url, if HasAttachment is true
Sep 18 '09 #2
ssnaik84
149 100+
calculate relative image path on the basis of image id/name
e.g.
Expand|Select|Wrap|Line Numbers
  1. protected string GetImageUrl(object imageIPath)
  2. {
  3. return "<img src='" + imagePath.ToString() + " />";
  4. }
Sep 18 '09 #3
Frinavale
9,735 Expert Mod 8TB
It would be even better if the path or url to the file was saved in the database :)

That way you could just place an Image control in the GridView and bind it to the URL...regardless, you need to add the URL somehow to the datasource you're using for your GridView or you're going to run into problems.
Sep 18 '09 #4

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

3 posts views Thread by CLEAR-RCIC | last post: by
3 posts views Thread by velu | last post: by
1 post views Thread by VMI | last post: by
1 post views Thread by hplayer03 | last post: by
2 posts views Thread by Paulo | last post: by
10 posts views Thread by gnewsgroup | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
2 posts views Thread by Marin | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.