简体   繁体   中英

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. Please help me with this. I want to hide a data row without deleting it in c# or 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 . Every DataTable has a DataView associated with it by default, in its DefaultView property. When you bind a DataTable , it is actually the DefaultView that the data comes from. As such, you can set the RowFilter of that DataView in order to hide rows that don't match certain criteria. For instance, if you had a Sex column that contained "Male" or "Female", you could hide all the "Male" rows like this:

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.

If you are binding, I would recommend using a BindingSource in between, in which case you set its Filter property in the same way.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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