如何在使用gridview绑定布尔值时显示“是”或“否”而不是复选框? [英] How to display “yes” or “no” instead of checkbox while binding boolean value with gridview ?

查看:84
本文介绍了如何在使用gridview绑定布尔值时显示“是”或“否”而不是复选框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="AdminPageDS" Width="70%" OnRowDataBound="GridView1_RowDataBound" BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px" CellPadding="3" AllowPaging="True">
            <Columns>
                <asp:TemplateField>
                    <HeaderTemplate>
                        <%--<input id="Checkbox1" type="checkbox" onclick="checkAll(this)"/>--%>
                        <asp:CheckBox ID="CheckBox1" runat="server" onclick="checkAll(this)" />
                    </HeaderTemplate>
                    <ItemTemplate>
                        <asp:CheckBox ID="CheckOne" runat="server"  Text="" onclick="Check_Click(this)" />
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:BoundField DataField="FirstName" HeaderText="First Name" SortExpression="FirstName" />
                <asp:BoundField DataField="LastName" HeaderText="Last Name" SortExpression="LastName" />
                <asp:CheckBoxField DataField="IsActive" HeaderText="IsActive" SortExpression="IsActive" />
                <asp:BoundField DataField="Email" HeaderText="Email" SortExpression="Email" />
                <asp:CheckBoxField DataField="IsSuperAdmin" HeaderText="IsSuperAdmin" SortExpression="IsSuperAdmin" />
                <asp:BoundField DataField="Department" HeaderText="Department" SortExpression="Department" />
                <asp:BoundField DataField="JobRole" HeaderText="Job Role" SortExpression="JobRole" />
                <asp:TemplateField>
                    <ItemTemplate>
                        <table>
                            <tr>
                                <td style="border: thin double #428bca; padding-right: 4px;">
                                    <asp:LinkButton ID="CtrlEdit" runat="server" CommandArgument='<%# Eval("UserID","{0}") %>' OnCommand="CtrlEdit_Command" Visible='<%# User.IsInRole("CreateUser") %>'>Edit</asp:LinkButton>
                                </td>
                                <td style="border: thin double #428bca;">
                                    <asp:LinkButton ID="CtrlDelete" runat="server" CommandArgument='<%# Eval("UserID","{0}") %>' OnCommand="CtrlDelete_Command" Visible='<%# User.IsInRole("CreateUser") %>'>Delete</asp:LinkButton>
                                </td>
                            </tr>
                        </table>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
            <FooterStyle BackColor="White" ForeColor="#000066" />
            <HeaderStyle BackColor="#428bca" Font-Bold="True" ForeColor="White" />
            <PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left" />
            <RowStyle ForeColor="#000066" />
            <SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" />
            <SortedAscendingCellStyle BackColor="#F1F1F1" />
            <SortedAscendingHeaderStyle BackColor="#007DBB" />
            <SortedDescendingCellStyle BackColor="#CAC9C9" />
            <SortedDescendingHeaderStyle BackColor="#00547E" />
            </asp:GridView>





我尝试过:



protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)

{

if(e.Row.RowType == DataControlRowType.DataRow)

{RsHunterDBEntities ctx =(RsHunterDBEntities)e。 Row.DataItem;

var checkIsActive = ctx.Users.Any(x => x.IsActive == true);

if(checkIsActive == true)

{

e.Row.Cells [2] .Text =是;



}

否则if(checkIsActive == false)

{

e.Row.Cells [2] .Text =No;

}

}

}



What I have tried:

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{ RsHunterDBEntities ctx = (RsHunterDBEntities)e.Row.DataItem;
var checkIsActive = ctx.Users.Any(x => x.IsActive == true);
if (checkIsActive == true)
{
e.Row.Cells[2].Text = "Yes";

}
else if(checkIsActive == false)
{
e.Row.Cells[2].Text = "No";
}
}
}

推荐答案

e.Row.Cells[2].Text



根据您的代码,Cell 2是 CheckBoxField 。所以,它不起作用。用 BoundField 替换此字段。它应该可以工作。


Here Cell 2 is a CheckBoxField according to your code. So, it is not working. Replace this field with a BoundField. It should work.


这篇关于如何在使用gridview绑定布尔值时显示“是”或“否”而不是复选框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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