Lets say you just brought back some data from a query and you want to rearrange the data or you simply have two datatables with the same schema that you want to join. Here is some sample code that will place the main customer first and then the additional customers sorted by customerId:
// Customers Object
clsCustomers objCustomers = new clsCustomers();
// Get Main Customer ID and Customer DataSet
int customerId = objCustomers.getMainCustomer();
DataSet dsCustomers = objCustomers.getCustomers();
// Create Temp Table to hold Main/Additional Customers sorted with Main Customer First
DataTable dtTempTable = new DataTable();
// Create Temp View to hold Main/Additional Customers
DataView dvCustomer;
// Setup Main Customer using RowFilter
dvCustomer = dsCustomers.Tables["Default"].DefaultView;
dvCustomer.RowFilter = "customerId = '" + customerId + "'";
// Add Main Customer to Temp Table
// The ToTable method is used to copy just the results of the RowFilter
dtTempTable = dvCustomer.ToTable();
// Setup Additional Customers using RowFilter and Sorting
dvCustomer = dsCustomers.Tables["Default"].DefaultView;
dvCustomer.RowFilter = "customerId <> '" + customerId + "'";
dvCustomer.Sort = "customerId";
// Add Additional Customers to Temp Table
// The Merge method is used to merge the two tables together
dtTempTable.Merge(dvCustomer.ToTable(), false, MissingSchemaAction.Add);
All Done! Now you have your DataTable dtTempTable sorted with the main customer first and then the rest of the customers sorted by customerId.
No comments:
Post a Comment