Posts Tagged ‘datatable’
Filter Datatable by Rows
If you need to get subset of a big dataset into datatable Rowwise …
Dim dtEmp as DataTable Dim dsEmployee as New DataSet dsEmployee = someobject.getdata() 'dataset can be populated in many ways which is not explained here. Dim sExpr as String Dim drRows() as DataRow, drSingleRow as DataRow sExpr = "EmpID > 100" 'Condition drRows = dsEmployee.Tables(0).Select(sExpr) 'If you need to add Sort Order it can be added to sExpr Dim sSortOrder as String sSortOrder = "EmpName DESC" drRows = dsEmployee.Tables(0).Select(sExpr,sSortOrder) For Each drSingleRow in drRows dtEmp.ImportRow(drRows) Next ' Datatable dtEmp has filtered records
Filter Datatable by Column
If you need to get subset of big dataset into datatable columnwise…
Dim dtEmp as DataTable Dim dsEmployee as New DataSet dsEmployoee = someobject.getdata() 'dataset can be populated in many ways which is not explained here. 'This copies the structure and data dtEmp = dsEmployee.Tables(0).Copy dtEmp.Columns.Remove("Unwanted Column 1") dtEmp.Columns.Remove("Unwanted Column 2") dtEmp.Columns.Remove("Unwanted Column 3")
Copying a datatable from one dataset to another was not that straightforward as I thought.
Dim dsSource As New DataSet Dim dsDestination As New DataSet 'Your way to get data from XML or DB. dsSource = object.getdata() dsDestination = object.getmoredata() 'This is important, without this it will result in error 'when you try to copy the datatable from one dataset to another dsSource.Tables(0).TableName = "NewTableName" dsDestination.Tables.Add(dsSource.Tables(0).Copy) dsDestination.Tables(0).Tablename = "SomeTable" dsDestination.Tables(1).Tablename = "NewTableName" 'The one we copied from other Dataset
Note 1 : When you are using more than one datatable in a dataset it is advisable you name the datatables.
Note 2 : These kinds of scenarios arise, when you are trying to establish Relation between datatables in a dataset.
(Cascading master – detail relation)