开发者

How do I replace a checkbox in gridview with text?

开发者 https://www.devze.com 2023-01-26 08:29 出处:网络
I have the following gridview: <asp:GridView ID=\"gdvReport\" runat=\"server\" AutoGenerateColumns=\"False\" DataSourceID=\"sdseport\">

I have the following gridview:

        <asp:GridView ID="gdvReport" runat="server" AutoGenerateColumns="False" DataSourceID="sdseport">
        <Columns>
            &l开发者_JAVA技巧t;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


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.


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.


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.


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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号