Setting DataGridViewRow.Visible = false at run time

Hello.

I'm tring to hide some roes at run time and get the following Exception:

InvalidOperationException,
Row associated with the currency manager's position cannot be made invisible.

the code is simple (this extends DataGridView:

 foreach (DataGridViewRow row in this.Rows)
            {                                      
                    row.Visible = IsRowShouldBeVisible(row.Index);
                
            }


The DataSource is a code DataTable object.

How can I hide the rows

Thanx Eli.


Answer this question

Setting DataGridViewRow.Visible = false at run time

  • Tomik

    That means that the row you are trying to make invisible is currently being edited. End all edits before you try to make the row invisible.

  • CodeStealer

    The code worked flawlessly... Thanks mate!!


  • mEt

    thanks a million...that one has been buggin me for a couple days now.

    Cheers


  • Mohammad Iqubal

    Hi,

    I am getting the same error although the code is a bit different but I am trying to do the same thing. Does anyone has an idea when and why this error occurs and how to fix it

    Thanks,

    Kunal


  • Nilsson

    try again!!! You need to free up the currency manager something like this...

    if (DGV.Rows.Count > 0){

    CurrencyManager cm = (CurrencyManager)BindingContext[DGV.DataSource];

    int rowCount = cm.Count;

    for (int row = 0; row < rowCount; ++row)

    {

    DataGridViewRow dgvr = DGV.Rows[row];

    if (dgvr.Cells[0].Tag.ToString() != "Within 6 months")

    {

    cm.SuspendBinding();//<-----Money Shot!!!!

    dgvr.Visible = false;

    }

    }

    }


  • Darkside

    I've uncovered the following problem with the solution above. In the DataBindingComplete event I was trying to make the row invisible.

    cm.SuspendBinding();
    for (int i = grdLineItems.Rows.Count - 1; i >= 0; i--)
    {
    if (Convert.ToBoolean(grdLineItems.RowsIdea.Cells["IgnorePricing"].Value))
    grdLineItems.RowsIdea.Visible = false;
    }
    }
    cm.ResumeBinding();


    The problem: If I add a row and then edit a cell, the CellValidated event is ignored. The order of events are:

    CellValidating
    DataBindingComplete
    CellValidated

    Does anyone know WHY the CellValidated event would NOT get fired when this occurs

    For me, this is bad because I have to hide rows, edit values, etc. with this grid.

    Stuart

  • DancnDude

    Thank-you!!!

    I used your sample code for a search feature in my application. Setting the rows to invisible hides the rows which are not suitable. When the user clicks 'reset' my code is as follows:

    Dim cm As CurrencyManager

    cm = CType(BindingContext(dgvOperators.DataSource), CurrencyManager)

    cm.ResumeBinding()

    cm.Refresh()

    txtSearch.Text = ""

    Cheers,

    Luc


  • Setting DataGridViewRow.Visible = false at run time