简体   繁体   English

如何隐藏数据表中的特定数据行而不删除它?

[英]How to hide particular data row in a datatable without deleting it?

I want to hide a data row without deleting it in c# or vb.net.我想在 c# 或 vb.net 中隐藏数据行而不将其删除。 Please help me with this.请帮我解决一下这个。 I want to hide a data row without deleting it in c# or vb.net.我想在 c# 或 vb.net 中隐藏数据行而不将其删除。

ResultSelection.Checked = False ' Checkbox
Dim Dt As DataTable
Dim dr as datarow
Private SBind As BindingSource = New BindingSource()
With Dt
     .Columns.Add(" Name", GetType(String))
     .Columns.Add("Age" , GetType(integer))
     .Columns.Add("Marks" , GetType(integer))

dr(0) = ( "Mark",19,99)
dr(1) =  ( "Rahul",20,35)
dr(2) =  ( "Steve",19,50)

SBind.DataSource = Dt
DatargridView.DataSource = SBind

if ResultSelection.Checked = False then
    "entire row dr(2) should not be visible
else
    "entire data table should be visible along with dr(2)
end if

You can't hide rows in a DataTable but you can in a DataView .您不能在DataTable中隐藏行,但可以在DataView中隐藏行。 Every DataTable has a DataView associated with it by default, in its DefaultView property.默认情况下,每个DataTable在其DefaultView属性中都有一个与之关联的DataView When you bind a DataTable , it is actually the DefaultView that the data comes from.当您绑定DataTable时,它实际上是数据来自的DefaultView As such, you can set the RowFilter of that DataView in order to hide rows that don't match certain criteria.因此,您可以设置该DataViewRowFilter以隐藏不符合某些条件的行。 For instance, if you had a Sex column that contained "Male" or "Female", you could hide all the "Male" rows like this:例如,如果您有一个包含“男性”或“女性”的Sex列,您可以像这样隐藏所有“男性”行:

myDataTable.DefaultView.RowFilter = "Sex = 'Female'"

Any controls bound to that DataTable would not see the "Male" rows and you could loop through the DataView code to see the filtered data too.绑定到该DataTable的任何控件都不会看到“男性”行,您也可以遍历DataView代码以查看过滤后的数据。

If you are binding, I would recommend using a BindingSource in between, in which case you set its Filter property in the same way.如果您正在绑定,我建议在两者之间使用BindingSource ,在这种情况下,您可以以相同的方式设置其Filter属性。

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

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