Sorting data in datagrid view

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
 
N

Nicholas Paldino [.NET/C# MVP]

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.
 
A

Ashutosh

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?
 
N

Nicholas Paldino [.NET/C# MVP]

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top