Home > Enterprise >  Vb.net - Filtering data tables with multiple columns
Vb.net - Filtering data tables with multiple columns

Time:10-12

Just started working with datatable, it has 21 columns(only 3 to 4 are needed to sort),and 67000 lines. So what I wanted to know was how to search for rows with those column then save it to another table.

:/ been at it 3weeks can't get past loading it to table. .. Wanting to find which rows are both 0 and type2.

X - TYPE - QTY.
‐-------------.
1 - type1 - 1.
1 - type2 - 0.
1 - type1 - 0.
1 - type1 - 1.
1 - type2 - 0.

I've used a for loop with a if statement trying to match the desired rows to then move desire rows for each line to new table.:/ so far stuck on sorting through part. Only working code is in a old vba project ment for excel.

CodePudding user response:

Assuming that you have a populated DataTable that can't be changed using the original query, you can use a DataView to sort and filter the data. You can't easily affect the data exposed via the Rows collection, but the DefaultView property is type DataView, which has Sort and RowFilter properties.

You mention sorting in your question but I suspect that that's a mistake and all you actually want is filtering. You can set the RowFilter to filter the data exposed via the DefaultView. You can then just access the data via that DataView, or you can call ToTable to generate a new DataTable based on that sort and filter, e.g.

oldTable.DefaultView.RowFilter = "Type = 'type2' AND Qty = 0"

Dim newTable = oldTable.DefaultView.ToTable()

Note that, when you bind a DataTable in WinForms, it is the DefaultView that the bound data is pulled from. If you don't want to filter the DefaultView of the table, e.g. you don't want to affect bound controls, then you can explicitly create a new DataView and use that.

  • Related