Click here to Skip to main content
15,893,622 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have a bit value (in Status) i want to display its status in gridview as if true, the row display "Active", otherwise the row display "Inactive", this is my code, but the result displays True or false itself. And based on the Active or Inactive, the Action column must display Deactivate or Activate
SQL
ShopNumber  ShopName    Address Website   Status    Action  Settings
2             abc        Kuwait xyz.com     True    Deactivate  Edit
3             def        Kuwait deuuy       False   Activate    Edit
4            Major       Minor  Usra.com    True    Activate    Edit
5             Isys       Kuwait isys.com    False   Activate    Edit
6            Avenues     Kuwait avenues.com False   Activate    Edit

Here is the codebehind:

C#
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {

            Boolean bitCheck = Convert.ToBoolean(e.Row.Cells[4].Text);
            if (bitCheck)
            {
                e.Row.Cells[4].Text = "Active";
            }
            else
            {
                e.Row.Cells[4].Text = "Inactive";
            }

        }


Here is ASP code:

ASP.NET
<asp:GridView ID="GridView1" runat="server" AllowPaging="True" 
        AutoGenerateColumns="False" DataSourceID="SqlDataSource1" 
         AllowSorting="True" 
         onselectedindexchanged="GridView1_SelectedIndexChanged" 
         DataKeyNames="TempID">

        <Columns>
            <asp:BoundField DataField="ShopNumber" HeaderText="ShopNumber" SortExpression="ShopNumber" >
            <ItemStyle Width="150px"></ItemStyle>
            </asp:BoundField>
            <asp:BoundField DataField="ShopName" HeaderText="ShopName" SortExpression="ShopName" >
            <ItemStyle Width="170px"></ItemStyle>
            </asp:BoundField>
            <asp:BoundField DataField="Address" HeaderText="Address" SortExpression="Address" >
            <ItemStyle Width="170px"></ItemStyle>
            </asp:BoundField>
            <asp:BoundField DataField="Website" HeaderText="Website" SortExpression="Website" >
            <ItemStyle Width="150px"></ItemStyle>
            </asp:BoundField>

            <asp:TemplateField HeaderText="Status" SortExpression="Status">
                <EditItemTemplate>
                    <asp:TextBox ID="textStatus" runat="server" Text='<%# Bind("Status") %>'></asp:TextBox>
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:Label ID="Label2" runat="server" Text='<%# Bind("Status") %>'></asp:Label>
                </ItemTemplate>
                <ItemStyle Width="150px" />
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Action" SortExpression="Action">
                <EditItemTemplate>
                    <asp:TextBox ID="textAction" runat="server" Text='<%# Bind("Action") %>'></asp:TextBox>
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:Label ID="Label1" runat="server" Text='<%# Bind("Action") %>'></asp:Label>
                </ItemTemplate>
                <ItemStyle Width="150px" />
            </asp:TemplateField>
            <asp:CommandField HeaderText="Settings" ShowEditButton="True" >
            <ItemStyle Width="150px"></ItemStyle>
            </asp:CommandField>
Posted
Updated 7-May-15 1:26am
v2

Hi,

Try this. It will work.

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                Label Label2 = (Label)e.Row.FindControl("Label2");
                Label Label1 = (Label)e.Row.FindControl("Label1");

                if (Convert.ToBoolean(Label2.Text.Trim()))
                {
                    Label2.Text = "Active";
                    Label1.Text = "Activate";
                }
                else if(!Convert.ToBoolean(Label2.Text.Trim()))
                {
                    Label2.Text = "Inactive";
                    Label1.Text = "Deactivate";
                }
            }
        }
 
Share this answer
 
Comments
zahedkw 7-May-15 6:51am    
ShopNumber int
ShopName nvarchar(50)
Address nvarchar(50)
Website nvarchar(50)
Status bit
Action nchar(10)
TempID int

I tried your solution but it didn't work. The values in the gridview still display True/False. Could you have a look once more.
Just Missing : OnRowDataBound="GridView1_RowDataBound"

In your Gridview control
SQL
<asp:GridView ID="GridView1" runat="server" AllowPaging="True"
        AutoGenerateColumns="False" DataSourceID="SqlDataSource1"
         AllowSorting="True"
         onselectedindexchanged="GridView1_SelectedIndexChanged"
         DataKeyNames="TempID">
 
Share this answer
 
Comments
zahedkw 7-May-15 7:27am    
Thanks it works

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900