简体   繁体   English

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

[英]How do I replace a checkbox in gridview with text?

I have the following gridview: 我有以下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. 第二行是数据库中的布尔值,但我不想向用户显示复选框或true \\ false。

How do I display something like this instead? 如何显示类似这样的内容? 0 = Don't Call 1 = Call Us 0 =不打电话1 =打电话给我们

You could create a TemplateField instead of a BoundField. 您可以创建一个TemplateField而不是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. 然后,您可以内联一些代码以显示所需的文本,或处理RowDataBound事件以执行此处的逻辑。

I ended up just using OnRowDataBound for this. 我最终只是为此使用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";
                }

            }
        }
    }
}

And it is working great now. 现在它运行良好。

You should do it in the sql instead of doing it here using a CASE statement such as 您应该在sql中执行此操作,而不是使用CASE语句在此处执行操作,例如

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" />

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" %>' Text ='<%#Eval(“ isActive”)== DBNull.Value?“ inactive”:Convert.ToBoolean(Eval(“ isActive”))?“ active”:“ inactive”%>'

Hope that helps. 希望能有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何根据图像中的文本替换Gridview列中的超链接? - How do I replace a Hyperlink in a Gridview Column with an image, depending on the text in the column? 我想在现有的复选框gridview表中添加新的复选框行,也想在数据库中添加该新的复选框行 - I want to add new checkbox rows in existing checkbox gridview table and also want to add that new checkbox row in database how will i do it 如何将复选框添加到 GridView? - How can I add Checkbox to GridView? 如何限制用户只选择我动态创建的gridview中的单个复选框 - How do I restrict user to select only single checkbox in gridview which i created dynamically 如何在选中复选框的嵌套gridview中获取字段的值? - How do I get value of field in nested gridview where checkbox is checked? 如何在ASP / C#中从gridview查找复选框的值 - How do I find the value of a checkbox from a gridview in asp/c# 如何基于同一行上的复选框选择来获取gridview上特定单元格的值? - How do I get the value of specific cells on a gridview based on checkbox selections on the same row? 如何将复选框的文本绑定到Expander的页眉 - How do I bind text of checkbox to Header of Expander 如何对GridView排序? - How do I sort a GridView? 如何用用户输入替换文本文件中的行的一部分? - How do I replace a part of a line in a text file with user input?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM