0

I have CheckedChanged event handler that hides a row from a gridview when the checkbox is checked. Now when I uncheck the checkbox, nothing happens. I actually want the row that gets hidden to be visible when the checkbox is unchecked. So I want the opposite to happen when I uncheck the checkbox. Currently the event handler does hide the row but I want the row to re-appear when I uncheck the checkbox

The checkbox:

 <div class="row mt-4 mb-2" style="width: 50%; margin: auto;">
                <div class="col-md-12">
                    <div class="check-label-Prof-Clients" >
                        <asp:CheckBox ID="chkProfClients" class="check-trading" runat="server" Enabled="true" AutoPostBack="true" 
                            ViewStateMode="Enabled" EnableViewState="true" Checked="false" OnCheckedChanged="chkProfClients_CheckedChanged"/>
                        <label class="form-check-label" style="font-size: 14px; font-weight: 400;" for="chkProfClients">
                                 Only Show Profitable Clients
                        </label>     
                    </div>
                </div>
            </div>

The checkbox CheckedChanged event handler:

protected void chkProfClients_CheckedChanged(object sender, EventArgs e)
{
    for (int i = 0; i <= gridProfitMargin.Rows.Count - 1; i++)
    {
        var profitValue = Convert.ToDouble(gridProfitMargin.Rows[i].Cells[13].Text);
        if (profitValue <= 0)
        {
            gridProfitMargin.Rows[i].Visible = false;
        }
        else
        {
            gridProfitMargin.Rows[i].Visible = true;
        }
    }       
}

I have "Autopostback = true". But still, nothing happens when I uncheck the checkbox

1 Answer 1

0

You run a loop, but you don't check if the check box is being checked, or un-checked.

So, you need to ALSO check the state/status of the check box, and based on if the check box = true, then the code and logic has to be different then the case when the check box - un-checked.

So, then this:

protected void chkProfClients_CheckedChanged(object sender, EventArgs e)
{
    for (int i = 0; i <= gridProfitMargin.Rows.Count - 1; i++)
    {
        var profitValue = Convert.ToDouble(gridProfitMargin.Rows[i].Cells[13].Text);
        if (chkProfClients.Checked)
        {
            if (profitValue > 0)
                gridProfitMargin.Rows[i].Visible = true;
            else
                gridProfitMargin.Rows[i].Visible = false;
        }
        else
        {
            // don't care - always show
            gridProfitMargin.Rows[i].Visible = true;
        }
    }
}

And, I suppose you could shorten the code a bit, say with this:

protected void chkProfClients_CheckedChanged(object sender, EventArgs e)
{
    for (int i = 0; i <= gridProfitMargin.Rows.Count - 1; i++)
    {
        var profitValue = Convert.ToDouble(gridProfitMargin.Rows[i].Cells[13].Text);
        if (chkProfClients.Checked)
        {
            gridProfitMargin.Rows[i].Visible = (profitvalue > 0);
        }
        else
        {
            // don't care - always show
            gridProfitMargin.Rows[i].Visible = true;
        }
    }
}

but, this comes down to what you find more readable here.

Try the first one. So, you have a loop for each row of the gridview, but you not taking into account if the check box is checked (true), or it is not.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.