Abhishek
Abhishek

Reputation: 329

Bind image in Gridview

I am using a gridview to display information on a page. The condition is that when I get Y from database result I need to bind /images/goldx.png Else /images/check.gif how can I do that I am using asp.net with vb.net as backend

<asp:GridView  ID="grdLocation" runat="server" Width="100%" AutoGenerateColumns="false"  >
   <Columns>
     <asp:TemplateField HeaderText="Monthly" HeaderStyle-HorizontalAlign="Center" ItemStyle-HorizontalAlign="Center">
     <ItemTemplate>
        <asp:Label ID="lblLotName" runat="server" Text='<%#Eval("is_monthly") %>'></asp:Label>
        <asp:Image ID="resultImage" runat="server" ImageUrl='<%# Eval("is_monthly") == 'Y'  ? "~/Images/check.gif" : "~/Images/goldx.png" %>' />
     </ItemTemplate>
   </asp:TemplateField>
 <Columns>
</asp:GridView>

My code for binding the Gridview :

Protected Function bindLocations()
    Try
        Dim _ds As DataSet
        If _locComp Is Nothing Then
            _locComp = New LocationComponent()
        End If
        _ds = _locComp.GetAllLots()

        If _ds.Tables(0).Rows.Count > 0 Then
            grdLocation.DataSource = _ds
            grdLocation.DataBind()

        End If
    Catch ex As Exception

    End Try

End Function

Thanks for your comment and answers .

Upvotes: 2

Views: 1397

Answers (1)

Bharadwaj
Bharadwaj

Reputation: 2553

You can do this check OnRowDataBound event of GridView. Like

Private Sub grdLocation_OnRowDataBound(sender As Object, e As GridViewRowEventArgs) Handles grdLocation.RowDataBound

 If e.Row.RowType = DataControlRowType.DataRow Then
  Dim lblLotNametxt as String = CType(e.Row.FindControl("lblLotName"),Label).Text
  If lblLotNametxt = "Y" Then
    CType(e.Row.FindControl("resultImage"),Image).ImageUrl = "~/Images/check.gif"
  Else
    CType(e.Row.FindControl("resultImage"),Image).ImageUrl = "~/Images/goldx.png"
  End If
 End If

End Sub

Hope this helps you.

Upvotes: 1

Related Questions