Click here to Skip to main content
16,018,419 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'd like to update the values of column Action based on the value of the column Status which is Boolean. If Status is True then value in Action must update to Deactivate. When I run, the Action field doesn't updates. I think there is an error in codebehind.

Here is the codebehind:
C#
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        string value = e.Row.Cells[4].Text;

        TextBox TextBox2 = (TextBox)e.Row.FindControl("TextBox2");
        if (value == "True")
        {
            TextBox2.Text = "Take";
        }
        else if (value == "False")
        {
            TextBox2.Text = "Available";
        }
    }

}


Here is the ASP code:

ASP.NET
<asp:GridView ID="GridView1" runat="server" AllowPaging="True"       AutoGenerateColumns="False" DataSourceID="SqlDataSource1" 
AllowSorting="True" 
onselectedindexchanged="GridView1_SelectedIndexChanged">       
<columns>
    <asp:BoundField DataField="ShopNumber" HeaderText="ShopNumber"     ItemStyle-Width="80" SortExpression="ShopNumber" >


<asp:BoundField DataField="ShopName" HeaderText="ShopName" ItemStyle-Width="80" SortExpression="ShopName" >


<asp:BoundField DataField="Address" HeaderText="Address" ItemStyle-Width="80"SortExpression="Address" >


<asp:BoundField DataField="Website" HeaderText="Website" ItemStyle-Width="80" SortExpression="Website" >


<asp:TemplateField HeaderText="Status" SortExpression="Status">
<itemtemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("Status") %>'>            
</itemtemplate>
<edititemtemplate>
<asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("Status") %>'>   
</edititemtemplate>
<itemstyle width="80px" />


<asp:TemplateField HeaderText="Action" SortExpression="Action">
<itemtemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("Action") %>'>
</itemtemplate>
<edititemtemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("Action") %>'> 
</edititemtemplate>
<edititemtemplate>
<asp:TextBox ID="TextBox1" runat="server">
</edititemtemplate>

</columns>
Posted
Updated 6-May-15 3:03am
v2

1 solution

TextBox2 is Status and TextBox1 is Action. You are doing it wrong.

It should be something like...
C#
TextBox txtAction = (TextBox)e.Row.FindControl("TextBox1");
TextBox txtStatus = (TextBox)e.Row.FindControl("TextBox2");

bool status;
            
if (Boolean.TryParse(txtStatus.Text, out status))
{
    txtAction.Text = "Take";
}
else
{
    txtAction.Text = "Available";
}

Note: Please change your control IDs to meaningful names, not TextBox1 and TextBox2.
 
Share this answer
 

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