如何根据数据库的真假值显示绿色和红色标志 [英] How to show green and red flag on the basis of true and false value from database

查看:168
本文介绍了如何根据数据库的真假值显示绿色和红色标志的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我有一个数据库表,其中我有标志列的bool值。



现在我想显示绿色标志为真值,红色标志为虚假值。



我尝试下面的代码,但我无法获取红旗我的gridview。



另外当他们在gridview中没有数据时我想在gridview中显示他们不是数据的消息。



我的尝试:



< asp:Content ID =BodyContentrunat = serverContentPlaceHolderID =MainContent>

< asp:GridView runat =ServerID =Gridview1AutoGenerateColumns =False

CellPadding =10 ForeColor =#333333DataKeyNames =RequestNumber,Flag

GridLines =None>



< columns>< asp:TemplateField HeaderText =Flag>

< itemtemplate>

< asp:ImageButton ID =ImageButton1runat =serverImageUrl =Images / True.jpgVisible ='<% #(bool)Eval(Flag)%>'/>

< asp:ImageButton ID =ImageButton2runat =serverImageUrl =Images / False.jpgVisible ='<%#!(bool)Eval(Flag)% >'/>




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") %>' />


推荐答案

更优选的是,你可以保留所有Gridview RowDataBound事件中关于此的逻辑。您可以执行以下操作:

More preferably you can keep the all logic regarding this in the Gridview RowDataBound event. You can do something like-
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
    }
}



用于在没有数据时显示消息 EmptyDataTemplate


For showing message when there is no data use EmptyDataTemplate

<emptydatatemplate>
    No data found!
</emptydatatemplate>





如果您还有任何疑问,请与我们联系。



希望,它有助于:)



Please let me know if you still have any queries.

Hope, it helps :)


这篇关于如何根据数据库的真假值显示绿色和红色标志的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆