简体   繁体   English

每当列值等于特定值时,如何隐藏 datagridview 行

[英]How can I hide datagridview row whenever a column value equals specific value

So for example,例如,

I have this datagridview which is bounded to a datatable:我有这个绑定到数据表的datagridview:

A一个 B C C
1 1 4 4 7 7
2 2 5 5 8 8
3 3 6 6 9 9

And B is the column I want to check the value for, for example I have a saved int value of 5. The code should check if in B column there is a value of 5, if true the all the rows with value 5 visibility is set to true, and all other rows will have their visibility set to false. B 是我要检查其值的列,例如我保存的 int 值为 5。代码应检查 B 列中是否有值 5,如果为真,则所有值为 5 的行可见性是设置为 true,所有其他行的可见性都将设置为 false。

Fow now, I have tried this little code (though it checks the all cells but not by specific column):现在,我已经尝试了这个小代码(尽管它检查所有单元格但不是按特定列):

            foreach (DataGridViewRow row in dataGridView1.Rows)
            {
                for (int i = 0; i < row.Cells.Count; i++)
                {
                    int rowIndex = row.Index;
                    if (row.Cells[i].Value.ToString().Equals("16"))
                    {
                        CurrencyManager currencyManager1 = (CurrencyManager)BindingContext[dataGridView1.DataSource];
                        currencyManager1.SuspendBinding();
                        dataGridView1.Rows[rowIndex].Visible = true;
                        currencyManager1.ResumeBinding();
                    }
                    else
                    {
                        CurrencyManager currencyManager1 = (CurrencyManager)BindingContext[dataGridView1.DataSource];
                        currencyManager1.SuspendBinding();
                        dataGridView1.Rows[rowIndex].Visible = false;
                        currencyManager1.ResumeBinding();
                    }
                        
                }
            }

Now about this CurrencyManager, when I tried to hide the rows without it, it simply error'd me, but now with it all rows visibility set to false.现在关于这个 CurrencyManager,当我试图隐藏没有它的行时,它只是让我出错,但现在所有行的可见性都设置为 false。 And the problem is I don't understand where to look at and how to fix it, even if the code checks the cells the value "16" in code should show some rows in datagridview but it doesn't.问题是我不明白在哪里查看以及如何修复它,即使代码检查单元格,代码中的值“16”应该在 datagridview 中显示一些行,但它没有。

It's a lot simpler.这要简单得多。 For a datagridview bound like this:对于像这样绑定的datagridview:

myDgv.DataSource = dt;

We can dynamically filter the dgv like this:我们可以像这样动态过滤 dgv:

dt.DefaultView.RowFilter = "[B] = 5"; //only show rows where B is 5

When a DGV's DataSource is set to a datatable it binds to the DataView exported by the DefaultView property so that you can change properties of the view like filter and sort, and it will influence the DGV.当 DGV 的 DataSource 设置为数据表时,它会绑定到 DefaultView 属性导出的 DataView,以便您可以更改视图的属性,如过滤器和排序,它会影响 DGV。 You also have the option of creating your own DataView based on the table and binding the DGV to that.您还可以选择基于表创建自己的 DataView 并将 DGV 绑定到该表。 For more info on the syntax you can use in a RowFilter, look at DataColumn.Expression有关可以在 RowFilter 中使用的语法的更多信息,请查看DataColumn.Expression

When working with bound data, do try to get into the habit of accessing the data by looking in the container (the datatable) rather than enumerating the DGV and pulling values out of it处理绑定数据时,请尽量养成通过查看容器(数据表)而不是枚举 DGV 并从中提取值来访问数据的习惯

You might find it more useful to bind your datatable to a BindingSource and then bind the BindingSource to the DGV- a bindingsource also has a Filter property with the same syntax but also maintains the notion of current row/ as the user changes the current row in the DGV the value of the Current property on the bindingsource changes, making it easier to manipulate the current row in code您可能会发现将数据表绑定到 BindingSource,然后将 BindingSource 绑定到 DGV 更有用 - 绑定源还具有具有相同语法的 Filter 属性,但在用户更改当前行时还保持当前行的概念/ DGV 绑定源上 Current 属性的值发生变化,从而更容易在代码中操作当前行


You indicate that this is all set up in the forms Designer, which means on your form you have:您指出这一切都是在 forms 设计器中设置的,这意味着在您的表单上您有:

  • a datagridview called _xDataGridView一个名为 _xDataGridView 的数据网格视图
  • a binding source called _xBindingSource一个名为 _xBindingSource 的绑定源
  • a dataset called _xDataSet一个名为 _xDataSet 的数据集
  • the bindingsource's DataSource is set to the dataset and the datamember is set to the table name in the dataset, and the datagridview's DataSource is set to the bindingsource bindingsource的DataSource设置为dataset,datamember设置为dataset中的表名,datagridview的DataSource设置为bindingsource

(mostly I'm just describing the setup to make sure it's right) (主要是我只是描述设置以确保它是正确的)

All you need to do is:您需要做的就是:

_xBindingSource.Filter = "[B] = 5";

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

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