Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Sorting within DBgrid

Posted on 2010-09-08
7
957 Views
Last Modified: 2013-11-23
Hi guys

i have numerous applications using BDE and dbgrids. my question is there an easy way to sort colums by clicking on the title of a particular column. i have found certain info for ado connections but not bde.

thank you in advance
steve
0
Comment
Question by:TG-Steve
7 Comments
 
LVL 14

Assisted Solution

by:SteveBay
SteveBay earned 100 total points
ID: 33628155
That depends upon your definition of “easy”. The *way* to sort a dbgrid is to change the order of the underlying dataset either through changing the active index or changing the order-by clause of the SQL statement
0
 

Author Comment

by:TG-Steve
ID: 33628446
when i query the sql database it returns several colums of data. i did think about creating several buttons on the form of which each of them run the same query but with different sort results. ideally i would like to have one button which returns my several columns of data and each person that uses the utility can sort specific data depending on what the individual wants. i.e one persons important data may be within column 3 and instead of reading jumbled information (which may be shop names randomly displayed accross 1000 lines) they can sort it by clicking on the column title and then it providing the user with a reliable set of data to work from.

i would do option 1 but there are 60+ people that use the data in different ways

thank you
steve
0
 
LVL 14

Assisted Solution

by:SteveBay
SteveBay earned 100 total points
ID: 33628533
You can still do it using clicks on the column headers but to sort the data you will need to re-query the database with the new sort order. You may want to store the sort order, per user, in an ini file or the registry so that user will have the same order the next time they display the grid.
 
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
LVL 9

Assisted Solution

by:Mahdi78
Mahdi78 earned 50 total points
ID: 33628874
use OnTitleClick of TDBGrid lke the following example

procedure TForm1.DBGrid1TitleClick(Column: TColumn);
begin
if Column.FieldName = 'MyField' then
(Column.Grid.DataSource.DataSet as TTable).IndexName := 'MyIndex';
end;
0
 
LVL 32

Assisted Solution

by:ewangoya
ewangoya earned 50 total points
ID: 33633021
Here is a solution I had posted for a similar question.
I just copied and pasted here.

The easiest way is to create a title on click event on the grid and then change your indexfields accordingly assuming your dataset is multidirectional or you are using a TClientDataset. Otherwise you can rerun the query with the new sort order obtained from the field you would like to sort on. Here is a simple example with a ADO, Of course there are better ways to keep track of your current sort field and order

TForm1 = class(TForm)
    ADOConnection1: TADOConnection;
    ADOTable1: TADOTable;
    DataSource1: TDataSource;
    DBGrid1: TDBGrid;
    ADOQuery1: TADOQuery;
    procedure DBGrid1TitleClick(Column: TColumn);
  private
    FCurrentSortField: string;
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.DBGrid1TitleClick(Column: TColumn);
var
  OrderByClause: string;
begin
  //if using multidirectional dataset
  if ADOTable1.IndexFieldNames = Column.FieldName then
    ADOTable1.IndexFieldNames := Column.FieldName + ' DESC'
  else
    ADOTable1.IndexFieldNames := Column.FieldName;

  //results are from a unidirectional dataset, then rerun your query with the new sort
  {
  ADOQuery1.DisableControls;
  try
    ADOQuery1.Active := False;
    if FCurrentSortField = Column.FieldName then
    begin
      OrderByClause := 'ORDER BY ' + Column.FieldName + ' DESC'
      FCurrentSortField := Column.FieldName + ' DESC';
    end
    else
    begin
      OrderByClause := 'ORDER BY ' + Column.FieldName
      FCurrentSortField := Column.FieldName;
    end;
    ADOQuery1.SQL.Add(' Your base query ');
    ADOQuery1.SQL.Add(OrderByClause);
    ADOQuery1.Active := True;
  finally
    ADOQuery1.EnableControls;
  end;
  }
end;
0
 
LVL 37

Accepted Solution

by:
Geert Gruwez earned 50 total points
ID: 33634042
actually the easiest way is to buy a component like TMSGrid or QuantumGrid
they have all that functionality built in (in memory sorting of the dataset)

http://www.tmssoftware.com/site/gridpack.asp
http://www.devexpress.com/Products/VCL/ExQuantumGrid/

there may be some work defining the grid/views/columns to get everything to work

and there is the possibility that your users like those grids very much
making them ask you to implement them everywhere :)
0
 

Author Comment

by:TG-Steve
ID: 33638929
Hi Guys

the information supplied by everyone is great thank you. i had a good think about my current path and decided to add checkboxes to the form. that way being at the time of query it will feed back given data with the desired sort order etc.

The component mentioned by Geert Gruwez looks very interesting and something i am going to look at going forward and possibly start from the ground up with it when i get the time.

thank you all in advance for the information
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Drag & Drop... Data from one grid to another 2 42
RESTRequest Parameter 4 43
Delphi: making a BW image transparent 10 61
Delphi: sending SMS on android platform 1 35
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question