Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Sort records in DBGrid? 1

Status
Not open for further replies.

GDGarth

Technical User
Aug 5, 2000
412
US
I know this should be easy, but how can I get records sorted in a dbgrid?

I've got the data coming from an Access database query, and the query is sorted correctly, but in my VB program, the data is displayed in a different order.

I even tried sorting the table, but that didn't make any difference.

There is no key column or index in the database table. When I display either the table or query in Access, it's sorted.

I'm a long way from an expert on the db controls, so I'm sure I'm just missing something easy.
 
You have to use a SQL statement for the recordsource of whatever is your data source for the grid with a ORDER by clause
You can fedd it in and change it at will while the program is running so you can see lots of different types of sorts using the same datagrid.
An easy way is to design the query in Access, copy the access sql text and paste it into the VB recordsource
If using code, just put quotation marks around the pasted access SQL otherwist paste it direct into the properties of the control that the datagrid is locked to.
If you change it with code, be sure to refresh the controls at the end of the changing sub to see the new sort order.
 
It works great, Thanks!

You get a Star!!!!
 
Because you are using a client side cursor, it may be more efficient to use ADO's Sort method

rsADO.Sort "myFieldName"

To match this up to your grid, such as when the user clicks a column header, you can do this:

rsADO.Sort = DataGrid1.Columns(2).DataField

Doing this will just sort on the field using the local cursor with-out having to requery or re-open the recordset again, and, in doing so, pulling all of the records across the net again.

If you use a DataControl, then you can use:

Adodc.Recordset.Sort = DataGrid1.Columns(2).DataField

or

Adodc.SortColumn = DataGrid1.Columns(2).DataField [/b][/i][/u]*******************************************************
General remarks:
If this post contains any suggestions for the use or distribution of code, components or files of any sort, it is still your responsibility to assure that you have the proper license and distribution rights to do so!
 
Thanks CCLINT I often wondered how I was supposed to use ADOSort
However I dont like clicking on columns like one does in Excel for ordinary users as it can get pretty confusing. I'd rather have option buttons or command icons to click on.
Do you know where I can get some decent sized option buttons or check boxes, the supplied dones are way too small on a big screen.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top