32

I have a DataGridView where I set DataSource:

taskerEntities te = new taskerEntities();
var OMsMasterDescriptiveIndicators = te.MyTable.Select(x => new lccls {Id = x.Id, name = x.name }).ToList();
MyGrid.DataSource = OMsMasterDescriptiveIndicators;

with my class lccls as

public class lccls
    {
        public string Id { get; set; }
        public Nullable<decimal> name { get; set; }
    }

At a certain event I want to make the current row invisible:

 MyGrid.Rows[5].Visible = false;

But I am unable to do this. Instead an exception is thrown with the following error message:

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

I suspect the reason is related to setting DataSource, but why?

Micha Wiedenmann
  • 19,979
  • 21
  • 92
  • 137
Amit Bisht
  • 4,870
  • 14
  • 54
  • 83
  • Where do you place the code `MyGrid.Rows[e.RowIndex].Visible = false;`? It's strange that if you use an arbitrary `index` such as `0`, `1`, ... it would hide the row successfully. – King King Sep 22 '13 at 09:02
  • i had changed e.rowindex but its still not working – Amit Bisht Sep 22 '13 at 09:07
  • Are you changing the datasource at runtime (deleting rows)? – varocarbas Sep 22 '13 at 09:18
  • I am not Deleting the row I am just Hiding the row – Amit Bisht Sep 22 '13 at 09:19
  • 1
    Yes, I got that. I meant if you are changing the Datasource at runtime For example: starting datasource 1, 2, 3; and you delete record 2; then you intend to make the given row invisible in the DGV. I have read your answer and most likely this is the problem. The safest way is performing any update directly from the DGV (or, when doing it from the datasource, making sure that you account for the variations in the DGV by accounting for the different indices involved). – varocarbas Sep 22 '13 at 09:44

8 Answers8

71

After searching a lot, I got the solution

CurrencyManager currencyManager1 = (CurrencyManager)BindingContext[MyGrid.DataSource];  
currencyManager1.SuspendBinding();
MyGrid.Rows[5].Visible = false;
currencyManager1.ResumeBinding();
Micha Wiedenmann
  • 19,979
  • 21
  • 92
  • 137
Amit Bisht
  • 4,870
  • 14
  • 54
  • 83
  • 9
    Thanks a lot. I am still in shok why hiding row is so complex ?!?!? – Lev Z Jun 02 '14 at 13:05
  • 5
    I had to change `CurrencyManager currencyManager1 = (CurrencyManager)BindingContext[MyGrid.DataSource];` to `CurrencyManager currencyManager1 = (CurrencyManager)MyGrid.BindingContext[MyGrid.DataSource]; ` to get it work – Shashank Shekhar Mar 26 '15 at 22:00
  • Yes it does work. And I think I know why I am running into it because I'm using multi-threading to populate the list which is my grid data source. My guess is that not all the threads are finished by the time I start trying to hide rows. – Shanerk Mar 14 '16 at 20:37
  • 8
    @Lev: _I am still in shock why hiding row is so complex ?!?!?_ It isn't; just set `CurrentCell = null`.. – TaW Jul 07 '17 at 15:06
17

Cannot set yourDataGridView row visible property to false when current row index Will encounter such error if trying to hide current cell

soulution :

when yourDataGridView Data source is not null :

  CurrencyManager currencyManager1 = (CurrencyManager)BindingContext[yourDataGridView.DataSource];
                       currencyManager1.SuspendBinding();
                       yourDataGridView.Rows[Target Index].Visible = false;
                       currencyManager1.ResumeBinding();

when yourDataGridView Data source is null :

 yourDataGridView.CurrentCell = null;
 yourDataGridView.Rows[Target Index].Visible = false;
Moory Pc
  • 860
  • 15
  • 16
  • 4
    +1 because your second solution worked for me. The first solution did **not** work even though I am bound to a DataSet and DataSource is not null! Go figure... – JonP Jun 20 '16 at 14:20
2

I have an example for U. I have a datagridview that may multiselected row. When i click the button to visible false row that selected. Try this:

foreach (DataGridViewRow row in dataGridView1.SelectedRows)
        {
            CurrencyManager currencyManager1 =(CurrencyManager)BindingContext[dataGridView1.DataSource];
                currencyManager1.SuspendBinding();
                dataGridView1.CurrentCell = null;
                row.Visible = false;
        }
        dataGridView1.Refresh();

Remember to set property SelectionMode: FullRowSelect

andand
  • 17,134
  • 11
  • 53
  • 79
  • You are forgetting to resume the CurrencyManager binding. Also, you only need to suspend the CurrencyManager once. You should suspend the CurrencyManager outside your update loop, and resume it after your loop. You don't need to set CurrentCell to null, either, and this shouldn't be done in the loop. You don't need to call Refresh() - the DataGridView will handle screen updates itself. – MadManMarkAu Apr 04 '19 at 01:41
  • 1
    My problem resolved by just adding the `dataGridView1.CurrentCell = null;` before the `row.Visible = false;`. No `SuspendBinding` was required. – PKanold Nov 28 '22 at 21:14
2

Example

        foreach (DataGridViewRow rw in dataGridView1.Rows)
        {

        if (rw.Cells[14].Value.ToString() == "") // this Cell have a TEXT, 
            {
                CurrencyManager currencyManager1 = (CurrencyManager)BindingContext[dataGridView1.DataSource];
                currencyManager1.SuspendBinding();
                rw.Visible = false; 
                currencyManager1.ResumeBinding();

            }
        }

this show only the row that have in the cell index 14, if this is blank or empty the whole row not show

0

Maybe a little late to answer this topic but I suggest you to use DataTable.DefaultView.RowFilter property to filter what you need to show on the bounded DataGridView. Please check the following link for more informtion: https://learn.microsoft.com/en-us/dotnet/api/system.data.dataview.rowfilter?redirectedfrom=MSDN&view=netframework-4.8#System_Data_DataView_RowFilter

regards.

0

I know this is an old topic, but an alternative solution (my code is vb.net, but I assume it would translate)

If WO_DGV.CurrentCell.RowIndex = i Then
  'you cannot make invisible the row that is 'current'
  WO_DGV.CurrentCell = WO_DGV.Rows(i - 1).Cells("act") 
  'to get to this code I know that there is a row before i, which is why I can use i-1 as new focus
End If
WO_DGV.Rows(i).Visible = False 
Hsmith
  • 155
  • 1
  • 1
  • 11
0

I tried to hide a row in a CellFormating event and it didn't worked. It looks like the currency maneger cann't be suspended for the row that raised the event than I deal with the row before (row 0 deals with the last row)

Private Sub DgView1_CellFormatting(sender As Object, e As DataGridViewCellFormattingEventArgs) Handles DgView1.CellFormatting
        If DgView1.Rows(e.RowIndex).Cells(e.ColumnIndex).OwningColumn.Name = CoID Then
            Dim k = If(e.RowIndex = 0, DgView1.RowCount - 1, e.RowIndex - 1)
            DgView1.Rows(k).Visible = Countries.Rows(k)("Ro")
        End If
End Sub
Amit Bisht
  • 4,870
  • 14
  • 54
  • 83
0

maybe this is old but:

Here an easy Way:

var item = dgv.Rows.Cast<DataGridViewRow>().FirstOrDefault(c => c.Cells[x].Value?.ToString() == X);
            if (item != null)
            {
                availableIpsDataGridView.CurrentCell = null;
                item.Visible = false;
            }

and visibling it with:

var item = dgv.Rows.Cast<DataGridViewRow>().FirstOrDefault(c => c.Cells[x].Value?.ToString() == X);
                if (item != null)
                {
                    item.Visible = true;
                }
Nima M
  • 1
  • 1
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Apr 05 '23 at 00:59