2016-12-16 80 views
0

我使用一个DataGridView在我的形式,它的数据源是绑定源控制。在绑定源控制的电流变化的情况下,我试图隐藏在DataGridView行。然后我得到以下错误,隐藏列一个DataGridView它的数据源是绑定源

与货币经理的位置关联的行不能被隐藏。

我用下面给出的代码,

rowClicked = reportsBindingSource.Position 
for (int i = 0; i < dgvItems.Rows.Count; i++) 
       { 
        try 
        { 
         if (rowClicked != i) 
         { 

          dgvItems.Rows[i].Visible = false; 
         } 

        } 
        catch (Exception) 
        { 

         throw; 
        } 

       } 

什么是错的代码?我尝试使用以下,但没有什么工作,

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

dgvItems.CurrentCell= null 
dgvItems.Rows[i].Visible = false; 

对此有任何解决方案?

+0

作为在异常指示的,隐藏在绑定的数据行模式不受支持。用例究竟是什么? –

+0

@Ivan Stoev我想躲除了在绑定源 –

+0

HMM的电流变化的情况下选择了一个所有行,所以你要始终显示在网格中只有当前项目从绑定列表?对于这些问题抱歉,但使用网格显示单个记录听起来很奇怪。 –

回答

0

那么,如例外所示,数据绑定模式不支持隐藏行。所以为了实现你的目标,你应该使用一些数据绑定机制。

从数据绑定的角度来看,“隐藏”的行相当于过滤源列表。由于BindingSource组件可以作为两个单项目和列表数据源起作用,最简单的方法是使用包含主源的Current中间BindingSource,像这样:

BindingSource bindingSource; // Your current component used as DataSource for the dataGridView 

var currentSource = new BindingSource { DataSource = bindingSource.Current }; 
dataGridView.DataSource = currentSource; 
bindingSource.CurrentChanged += (_sender, _e) => currentSource.DataSource = bindingSource.Current; 
+0

这将解决这一问题尤其是在我的情况,但正如你所说的加载一个DataGridView只有一个项目是不是更明显。我正在考虑其他解决方案 –