MrDysprosium
MrDysprosium

Reputation: 499

Trying to hide a row in my DataGridView when a bool is changed to true

I've seen a few questions on this very topic posted on SO, but the proposed fix has no change in my code.

Here's what I have.

    public void cell_formatting(object sender, System.Windows.Forms.DataGridViewCellFormattingEventArgs e)
    {

     //lots of work
         if (dataGridView.Columns[e.ColumnIndex].Name.Equals("Helped"))
        {
            if (e.Value.Equals(true))
            {

                CurrencyManager cm = (CurrencyManager)dataGridView.BindingContext[dataGridView.DataSource];
                cm.SuspendBinding();
                dataGridView.Rows[e.RowIndex].Visible = false; //Error here
                cm.ResumeBinding();

            }

Additional information: Row associated with the currency manager's position cannot be made invisible.

The above is almost verbatim the fix for most other users, any guesses to why it's not working for me?

Edit:

I've added a new line to clear the CurrentCell which now provides a new error;

            if (dataGridView.Columns[e.ColumnIndex].Name.Equals("Helped"))
        {
            if (e.Value.Equals(true))
            {
                CurrencyManager cm = (CurrencyManager)dataGridView.BindingContext[dataGridView.DataSource];
                cm.SuspendBinding();
                dataGridView.CurrentCell = null;
                dataGridView.Rows[e.RowIndex].Visible = false;
                cm.ResumeBinding();
            }
        }

Operation is not valid because it results in a reentrant call to the SetCurrentCellAddressCore function.

Upvotes: 0

Views: 517

Answers (1)

MrDysprosium
MrDysprosium

Reputation: 499

Well, as people have commented it looks like the posted code isn't the problem. Something else in my code is causing this to not work.

Here is what I ended up doing to fix it.

            if (dataGridView.Columns[e.ColumnIndex].Name.Equals("Helped"))
        {
            if (e.Value.Equals(true))
            {

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


                this.BeginInvoke(new MethodInvoker(() =>
                {
                    cm.SuspendBinding();
                    dataGridView.Rows[e.RowIndex].Visible = false;
                    RemoveRecord(dataGridView.Rows[e.RowIndex].DataBoundItem as Record);
                    cm.ResumeBinding();
                }));

Upvotes: 0

Related Questions