Click here to Skip to main content
15,899,313 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi All,

I have a database table in which I have bool value for flag column.

Now I want to show Green flag for true value and Red flag for false value.

I try below code but I am not able to fetch red flag in my gridview.

Also when their is no data in the gridview I want to show a message that their is not data in the gridview.

What I have tried:

<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
<asp:GridView runat ="Server" ID="Gridview1" AutoGenerateColumns="False"
CellPadding="10" ForeColor="#333333" DataKeyNames="RequestNumber,Flag"
GridLines="None">

<columns><asp:TemplateField HeaderText="Flag">
<itemtemplate>
<asp:ImageButton ID="ImageButton1" runat="server" ImageUrl="Images/True.jpg" Visible='<%# (bool)Eval("Flag") %>'/>
<asp:ImageButton ID="ImageButton2" runat="server" ImageUrl="Images/False.jpg" Visible='<%# !(bool)Eval("Flag") %>' />


Posted
Updated 31-Jul-16 21:09pm

1 solution

More preferably you can keep the all logic regarding this in the Gridview RowDataBound event. You can do something like-
C#
protected void MyGrid_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        ImageButton btn = (ImageButton )e.Row.FindControl("ImageButton1");
        btn.Visible= bool.Parse(DataBinder.Eval(e.Row.DataItem, "Flag").ToString());
        //any other logic as per your requirement
    }
}

For showing message when there is no data use EmptyDataTemplate
HTML
<emptydatatemplate>
    No data found!
</emptydatatemplate>


Please let me know if you still have any queries.

Hope, it helps :)
 
Share this answer
 
Comments
Karthik_Mahalingam 1-Aug-16 4:59am    
5!
Suvendu Shekhar Giri 1-Aug-16 5:22am    
Thanks Karthik :)
amitesh1989 3-Aug-16 6:06am    
@Suvendu can you please elaborate in details

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