views:

93

answers:

2

I have got a datatable which contains many columns in which three are main:

  1. hotelid
  2. dshotelid
  3. hotelname

Some hotels contain only dshotelid, some contains only hotelid and some contain both dshotelid and hotelid.

I need sort in such way so that those hotels who got both dshotelid and hotelid should be on top and then those hotels who have got only dshotelid and at last those hotels who have got only hotelid...

Please help me to create such a sort expression.

I created this :

dtmaintable.DefaultView.Sort = "dshotelid, hotelid"

but it Is not giving me desired output.

+1  A: 

Fields with NULL in them tend to appear at the top of sorted columns (unless the sort is in descending order) so you're probably seeing rows with both hotelid and dshotelid at the bottom, instead of the top?

How is this data table populated? If it's coming from a database query, it's a simple matter to construct an additional column (or columns) which contain(s) whatever sort key you need - be it an amalgum of other columns or some other unique identifier.

EDIT: Feb 4, 2010 - in response to your 'FilterAndSortTable' solution:

Your solution works but it's not because of the FilterAndSortTable - it's because you used a different sort order.

Originally, you used "dsohtelid, hotelid".

The second time, you used "dshotelid desc, hotelid desc".

This has the effect of putting your non-nulls at the top and nulls at the bottom, but I would dispute that this qualifies as a good solution. Your id's are now sorted backwards - which I kind of assumed you might want to avoid, hence my suggestion of a new sort column that would respect this.

Still, if the order of id's doesn't matter then your solution is fine and you can simply stick with your original code, with the addition of 'desc', like so:

dtmaintable.DefaultView.Sort = "dshotelid desc, hotelid desc"

Of course, if anything I've said here has been of any use to you, a tick would be muchly appreciated. It would help me reach 50 rep points and finally be able to write comments. :)

GenericMeatUnit
thanks but.. it not solved my problem
Rajesh Rolen- DotNet Developer
id's order is not matter for me.... and my solutions is working fine
Rajesh Rolen- DotNet Developer
Thanks for the upvote. :)
GenericMeatUnit
A: 

i got the answer its :

 datagrid1.datasource = FilterandSortTable(dtmaintable,"", "dshotelid desc, hotelid desc")

 Public Shared Function FilterandSortTable(ByVal SourceTable As DataTable, ByVal strFilters As String, Optional ByVal strOrder As String = "") As DataTable
    Dim Tbl As DataTable = SourceTable.Clone
    Dim rows() As DataRow = SourceTable.Select(strFilters, strOrder)
    For i As Integer = 0 To rows.Length - 1
        Tbl.ImportRow(rows(i))
    Next
    Return Tbl
End Function
Rajesh Rolen- DotNet Developer
well its giving me desired output but, Please check it and confirm me that i did correct or not...
Rajesh Rolen- DotNet Developer
I've ammended my previous answer to talk about this solution.
GenericMeatUnit