Datatable and Icomparer

raizen277

New member
Joined
Jun 22, 2006
Messages
2
Programming Experience
1-3
Is there a way to pass in rows of records into the Icomparer to sort them based on dates? I manage to sort usingIicomparer with a ListView but it doesn't work fully well. The problem is here: 1) There are 2 datasets with corresponding DataTable. 2) I add them both up as 1 dataset 3) I bind the values into a ListView and sort the items based on the first column date 4) I then get the total quantity of stock (for example) 5) Then starting from the top to bottom, i minus/add the total stock based on a column in the row and add it into another column to show the balance. The problem is that the items are added as though the items haven't been sorted yet. Please help
 
You don't sort DataTables. The order of the data in a DataTable doesn't change. That's what DataViews are for. Two very important points to note:

1. Every DataTable is implicitly associated with a DataView via its DefaultView property.
2. When you bind a DataTable to a control it is the contents of the DefaultView that gets displayed.

These two points together mean that all you have to do is set the Sort property of a DataTable's DefaultView and any bound controls will automatically be sorted. The DataView.Sort property takes a string that represents an SQL ORDER BY clause. In your case that may look something like this:
VB.NET:
Expand Collapse Copy
myDataTable.DefaultView.Sort = "DateColumn ASC"
 
Thanks! I actually managed to sort it already with the Icomparer on the ListView control by date and insert certain values after that. Thanks a bunch as I get more insight with the DataTable and DataView.
 
Note that if you intend to sort a ListView containing data from a DataTable I recommend assigning the DataRow that corresponds to each ListViewItem to that item's Tag property. That way you still have easy access to the row that corresponds to an item even if the ListView ordering doesn't match the DataTable or its DefaultView.
 
Back
Top