Reputation: 1207
I have data like this in database table:
vehicle1 driver1
vehicle1 driver2
vehicle2 driver3
vehicle3 driver4
vehicle3 driver5
vehicle4 driver6
vehicle4 driver7
vehicle4 driver8
I need to show drivers ordered by vehicles in asp.net dropdownlist. But I need to show drivers from vehicle X first where X is set up dynamically (programmatically).
So if X is vehicle3 the data in my dropdownlist would look like this:
**vehicle3 driver4**
**vehicle3 driver5**
vehicle1 driver1
vehicle1 driver2
vehicle2 driver3
vehicle4 driver6
vehicle4 driver7
vehicle4 driver8
Any suggestions how to accomplish this?
Upvotes: 0
Views: 149
Reputation: 46661
Order your results from the database by using
ORDER BY Vehicle, Driver
Then insert your dynamic items programmatically at index 0, so they appear before the data bound items.
Upvotes: 0
Reputation: 33839
If your data in SQL Server
database then select the data as required.
SELECT A.Vehicle, A.Driver
FROM (SELECT Vehicle, Driver, CASE @X WHEN Replace(Vehicle,'Vehicle','')
THEN 0 ELSE 1 END AS OrderCol
FROM yourTable ) A
ORDER BY A.OrderCol,A.Vehicle
Upvotes: 2