Sorting data in datagrid view

  • Thread starter Thread starter Ashutosh
  • Start date Start date
A

Ashutosh

Hi,
I have a table in which one column is declared as "laneID char(3)", but
it stores integer value. - This database was designed around 12 years
back...So, I don't know the reason for this & this can't be change under
any circumstances, no matter what.

Now, I am displaying the data of this table in a data grid view. So, for
sorting the data on this column, I changed the select command of the
adapter to

select laneID, lanename <some other columns also> from lanes order by
convert(int, laneID).

With this select query, lane IDs are sorted like integer. But when the
user clicks on the column header of laneID, the sort order is gone (it's
sorted like char) . How can I sort it in ascending and descending order
both, for/on this column treating them integers. I also need to allow
the users to sort data on other column in the usual way.

Thanks & Regards,
Ashutosh
 
Ashutosh,

You might want to select the data normally, and then create a computed
column on the data set that is returned. This computed column would be a
computed value with the parsed value of the integer.

You would then display that, and it should sort correctly.

Another option is to have the query create a new computed column that
does the conversion on the DB side and then display that column instead.
 
Thanks Nicholas,
Sounds good, but if I display the computed column in the grid, how will
the user add new rows using the data grid view?
 
Ashutosh,

Before updating (or when the column is changed), you would have to
generate a value for the original column that the computed column is based
on.
 
Back
Top