无法设置数据网格视图的行可见false

我有一个DataGridView ,我在其中设置DataSource

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

和我的class lccls一样

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

在某个事件中,我想让当前行不可见:

  MyGrid.Rows[5].Visible = false; 

但我无法做到这一点。 而是抛出exception,并显示以下错误消息:

与货币经理的头寸相关联的行不能隐藏

我怀疑原因与设置DataSource ,但为什么呢?

经过大量搜索,我得到了解决方案

 CurrencyManager currencyManager1 = (CurrencyManager)BindingContext[MyGrid.DataSource]; currencyManager1.SuspendBinding(); MyGrid.Rows[5].Visible = false; currencyManager1.ResumeBinding(); 

当前行索引在尝试隐藏当前单元格时会遇到此类错误,无法将yourDataGridView行的visible属性设置为false

洗液:

当yourDataGridView数据源不为null时:

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

当yourDataGridView数据源为null时:

  yourDataGridView.CurrentCell = null; yourDataGridView.Rows[Target Index].Visible = false; 

我有一个U的例子。我有一个可能多选行的datagridview。 当我单击按钮以显示选中的假行时。 试试这个:

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

请记住设置属性SelectionMode:FullRowSelect