0

Please forgive me if I do not articulate this in the most comprehensive way, I am new to .net.

What I am trying to do is: Have a checkbox external to a grid-view that once selected, updates the grid-view to ONLY display rows with a certain value. In this case either "YES" or "NO". Once the user selects the checkbox, it will only display rows that have column value of "YES".

I would prefer to do this in a way that does not require recreating the entire grid-view, but rather only displaying the rows that have the "YES" value in them. This is all I've gotten so far and I don't know if I'm on the right path:

protected void chkbox_Click(object sender, EventArgs e)
{
    AppError curError = null;
    User curUser = null;

    if (chkboxlbl.Checked == true) //if checked then jump into next step
    {
        foreach (GridViewRow row in GridViewAppts.Rows)
        {
          // not sure what to put here yet. 
        }
    }
}

3 Answers 3

2

By iterating each row you just need one more step to achieve the objective by setting row visibility to false: to find out in which column/cell index that "NO" values are set.

You can use one of two possible solutions below to hide all rows which contains "NO" value in certain column/cell index:

1) Using foreach loop

foreach (GridViewRow row in GridViewAppts.Rows)
{
    if (row.RowType == DataControlRowType.DataRow)
    {
        // 'n' belongs to column index, starting from leftmost column = 0
        if (row.Cells[n].Text == "NO")
        {
            row.Visible = false;
        }
    }
}

2) Using for loop

for (int i = 0; i < GridViewAppts.Rows.Count; i++)
{
    // 'n' belongs to column index, starting from leftmost column = 0
    if (GridViewAppts.Rows[i].Cells[n].Text == "NO")
    {
        GridViewAppts.Rows[i].Visible = false;
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

This is perfect. Thank you so much. I really appreciate it.
1

Set the Visible property of the row to False.

if(GridViewRow.Column = "NO")
    GridViewRow.Visible = False;

2 Comments

This is what I was looking for! One more question if you dont mind...I plugged that into the method but im getting a few errors. This is what I have now ------ try { if (chkboxlbl.Checked == true) { if(GridViewRow.Column = "NO") GridViewRow.Visible = false; } else { GridViewRow.Visible = true; } }
Im getting an error stating "GridViewRow does not contain a definition for 'Column'
0

You could try:

row.Attributes["style"] = "display:none";

If that doesn't work then I'd rebind. You could also do it client side.

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.