如何用文本替换 gridview 中的复选框?

发布于 2024-10-04 18:51:37 字数 602 浏览 2 评论 0原文

我有以下 gridview:

        <asp:GridView ID="gdvReport" runat="server" AutoGenerateColumns="False" DataSourceID="sdseport">
        <Columns>
            <asp:BoundField DataField="Phone" HeaderText="Phone" SortExpression="Phone">
                <ControlStyle Width="250px" />
            </asp:BoundField>
            <asp:BoundField DataField="ToCall" HeaderText="Foramt" SortExpression="ToCall" />
        </Columns>
    </asp:GridView>

第二行是数据库中的布尔值,但我不想向用户显示复选框或 true\false。

我该如何显示类似的内容? 0 = 不呼叫 1 = 致电我们

I have the following gridview:

        <asp:GridView ID="gdvReport" runat="server" AutoGenerateColumns="False" DataSourceID="sdseport">
        <Columns>
            <asp:BoundField DataField="Phone" HeaderText="Phone" SortExpression="Phone">
                <ControlStyle Width="250px" />
            </asp:BoundField>
            <asp:BoundField DataField="ToCall" HeaderText="Foramt" SortExpression="ToCall" />
        </Columns>
    </asp:GridView>

The second row is a bool in the database, but I do not want to show a checkbox or true\false to the users.

How do I display something like this instead?
0 = Don't Call
1 = Call Us

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

蝶…霜飞 2024-10-11 18:51:37

您可以创建 TemplateField 而不是 BoundField。

<asp:TemplateField HeaderText="Whatever">
    <ItemTemplate>
        <asp:Literal ID="litTextValue" runat="server" />
    </ItemTemplate>
</asp:TemplateField>

然后,您可以内嵌一些代码来显示所需的文本或处理 RowDataBound 事件以执行逻辑。

You could create a TemplateField instead of a BoundField.

<asp:TemplateField HeaderText="Whatever">
    <ItemTemplate>
        <asp:Literal ID="litTextValue" runat="server" />
    </ItemTemplate>
</asp:TemplateField>

You could then put some code inline to display the text that you want or handle the RowDataBound event to do the logic there.

爱人如己 2024-10-11 18:51:37

我最终只是使用 OnRowDataBound 来实现此目的。

<asp:GridView ID="gdvReport" runat="server" AutoGenerateColumns="False" DataSourceID="sdseport" OnRowDataBound="OnRowDataBound">
    <Columns>
        <asp:BoundField DataField="Phone" HeaderText="Phone" SortExpression="Phone">
            <ControlStyle Width="250px" />
        </asp:BoundField>
        <asp:BoundField DataField="ToCall" HeaderText="Foramt" SortExpression="ToCall" />
    </Columns>
</asp:GridView>


protected void OnRowDataBound(object sender, EventArgs e)
{  
    GridViewRowEventArgs ea = e as GridViewRowEventArgs;
    if (ea.Row.RowType == DataControlRowType.DataRow)
    {
        DataRowView drv = ea.Row.DataItem as DataRowView;
        Object ob = drv["Phone"];
        if (!Convert.IsDBNull(ob))
        {
            bool iParsedValue = false;
            if (bool.TryParse(ob.ToString(), out iParsedValue))
            {
                TableCell cell = ea.Row.Cells[1];
                if (iParsedValue == false)
                {

                    cell.Text = "Don't Call";
                }
                else
                {
                    cell.Text = "Call Us";
                }

            }
        }
    }
}

现在效果很好。

I ended up just using OnRowDataBound for this.

<asp:GridView ID="gdvReport" runat="server" AutoGenerateColumns="False" DataSourceID="sdseport" OnRowDataBound="OnRowDataBound">
    <Columns>
        <asp:BoundField DataField="Phone" HeaderText="Phone" SortExpression="Phone">
            <ControlStyle Width="250px" />
        </asp:BoundField>
        <asp:BoundField DataField="ToCall" HeaderText="Foramt" SortExpression="ToCall" />
    </Columns>
</asp:GridView>


protected void OnRowDataBound(object sender, EventArgs e)
{  
    GridViewRowEventArgs ea = e as GridViewRowEventArgs;
    if (ea.Row.RowType == DataControlRowType.DataRow)
    {
        DataRowView drv = ea.Row.DataItem as DataRowView;
        Object ob = drv["Phone"];
        if (!Convert.IsDBNull(ob))
        {
            bool iParsedValue = false;
            if (bool.TryParse(ob.ToString(), out iParsedValue))
            {
                TableCell cell = ea.Row.Cells[1];
                if (iParsedValue == false)
                {

                    cell.Text = "Don't Call";
                }
                else
                {
                    cell.Text = "Call Us";
                }

            }
        }
    }
}

And it is working great now.

夏の忆 2024-10-11 18:51:37

我这样做了,它起作用了,

     <asp:Literal ID="isActive" runat="server"
     Text='<%#Eval("isActive")==DBNull.Value ?
     "inactive":Convert.ToBoolean(Eval("isActive"))?"active":"inactive"
     %>'></asp:Literal>

这是重要的部分。

Text='<%#Eval("isActive")==DBNull.Value?"inactive":Convert.ToBoolean(Eval("isActive"))?"active":"inactive" %>'

希望有帮助。

I did this and it work

     <asp:Literal ID="isActive" runat="server"
     Text='<%#Eval("isActive")==DBNull.Value ?
     "inactive":Convert.ToBoolean(Eval("isActive"))?"active":"inactive"
     %>'></asp:Literal>

This is the important part.

Text='<%#Eval("isActive")==DBNull.Value?"inactive":Convert.ToBoolean(Eval("isActive"))?"active":"inactive" %>'

Hope that helps.

日久见人心 2024-10-11 18:51:37

您应该在 sql 中执行此操作,而不是在此处使用 CASE 语句执行此操作
例如

CASE ToCall WHEN '1' THEN 'Call' ELSE 'Do not call' END AS ToCall

,然后使用一个简单的绑定字段,例如

<asp:BoundField DataField="ToCall" HeaderText="Foramt" SortExpression="ToCall" />

You should do it in the sql instead of doing it here using a CASE statement
such as

CASE ToCall WHEN '1' THEN 'Call' ELSE 'Do not call' END AS ToCall

and then use a simple bound field such as

<asp:BoundField DataField="ToCall" HeaderText="Foramt" SortExpression="ToCall" />
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文