简体   繁体   English

隐藏在Datagridview中的列,该列的数据源是绑定源

[英]Hiding Column In a Datagridview which's data source is a binding source

I am using a datagridview in my form, which's data source is a binding source control. 我在表单中使用了datagridview,该数据源是一个绑定源控件。 In the current changed event of the binding source control I am trying to hide the rows in the datagridview. 在绑定源控件的当前更改事件中,我试图隐藏datagridview中的行。 Then I get the following error, 然后我得到以下错误,

Row associated with the currency manager's position cannot be made invisible. 与货币经理的头寸相关的行不能不可见。

The code i used is given below, 我使用的代码如下所示,

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

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

                    }
                    catch (Exception)
                    {

                        throw;
                    }

                }

What is wrong with the code? 代码有什么问题? I tried using the below but nothing works, 我尝试使用以下内容,但没有任何效果,

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

and

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

Is there any solution for this? 有什么解决办法吗?

Well, as indicated by the exception, hiding rows is not supported in data bound mode. 嗯,正如例外所指出的那样,在数据绑定模式下不支持隐藏行。 So in order to achieve your goal, you should use some data binding mechanism. 因此,为了实现您的目标,您应该使用一些数据绑定机制。

From data binding perspective, "hiding" rows is equivalent of filtering the source list. 从数据绑定的角度来看,“隐藏”行等同于过滤源列表。 Since the BindingSource component can act as both single item and list data source, the easiest is to use intermediate BindingSource containing the Current of the primary source, like this: 由于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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM