View Issue Details

IDProjectCategoryView StatusLast Update
0002096JEDI VCL00 JVCL Componentspublic2005-04-01 05:51
ReporterdavalvAssigned Toobones 
PrioritynormalSeveritymajorReproducibilityalways
Status resolvedResolutionfixed 
Product Version 
Target VersionFixed in Version3.00 
Summary0002096: JvDBGrid and sorting lookup fields
DescriptionLinking JvDBGrid with datasets containing lookup fields and on sorting lookup fields (by clicking column title) launch exception and doesn't sort column.
Additional InformationThis is the OnTitleClick code for JvDBGrid:



procedure TForm_Barche.BarcheDBGridTitleClick(Column: TColumn);
begin
  BarcheDBGrid.SortedField:= Column.FieldName;

  if Main_DataModule.BarcheADOTable.Sort = BarcheDBGrid.SortedField + ' ASC' then begin
    try
      Main_DataModule.BarcheADOTable.Sort:= BarcheDBGrid.SortedField + ' DESC';
    except
    end;
    BarcheDBGrid.SortMarker:= smDown;
  end
  else begin
    try
      Main_DataModule.BarcheADOTable.Sort:= BarcheDBGrid.SortedField + ' ASC';
    except
    end;
    BarcheDBGrid.SortMarker:= smUp;
  end;
end;
TagsNo tags attached.

Activities

davalv

2004-08-26 02:51

reporter   ~0005095

The exception is launched in JvDBGrid unit in this function:



procedure TJvDBGrid.MouseUp(Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
var
  Cell: TGridCoord;
  ACol: Longint;
  DoClick: Boolean;
begin
  Cell := MouseCoord(X, Y);
  if FTracking and (FPressedCol <> nil) then
  begin
    DoClick := PtInRect(Rect(0, 0, ClientWidth, ClientHeight), {Types.} Point(X, Y)) and
      (Cell.Y < TitleOffset) and
      (FPressedCol = GetMasterColumn(Cell.X, Cell.Y));
    StopTracking;
    if DoClick then
    begin
      ACol := Cell.X;
      if dgIndicator in Options then
        Dec(ACol);
      if (DataLink <> nil) and DataLink.Active and (ACol >= 0) and
        (ACol < Columns.Count) then
        DoTitleClick(FPressedCol.Index, FPressedCol.Field);
    end;
  end
  else
  if FSwapButtons then
  begin
    FSwapButtons := False;
    MouseCapture := False;
    if Button = mbRight then
      Button := mbLeft;
  end;
  // Polaris
  if (Button = mbLeft) and (FGridState = gsColSizing) and
    (FSizingIndex + Byte(not (dgIndicator in Options)) <= FixedCols) then
// and not AutoSizeColumns
  begin
    ColWidths[FSizingIndex] := GetMinColWidth(X - FSizingOfs - CellRect(FSizingIndex, 0).Left);
    FGridState := gsNormal;
// Exit;
  end;
  // Polaris

  if FTitleArrow and (Button = mbLeft) and
    (dgTitles in Options) and (dgIndicator in Options) and
    (Cell.X = 0) and (Cell.Y = 0) and DataLink.Active then
    ShowSelectColumnClick; // Selection of columns

  inherited MouseUp(Button, Shift, X, Y); // <=================== EXCEPTION
  DoAutoSizeColumns;
end;

davalv

2004-08-26 03:06

reporter   ~0005096

This is a better code:



procedure TForm_Barche.BarcheDBGridTitleClick(Column: TColumn);
begin
  BarcheDBGrid.SortedField:= Column.FieldName;

  if TADOTable(BarcheDBGrid.DataSource.DataSet).Sort = BarcheDBGrid.SortedField + ' ASC' then begin
    TADOTable(BarcheDBGrid.DataSource.DataSet).Sort:= BarcheDBGrid.SortedField + ' DESC';
    BarcheDBGrid.SortMarker:= smDown;
  end
  else begin
    TADOTable(BarcheDBGrid.DataSource.DataSet).Sort:= BarcheDBGrid.SortedField + ' ASC';
    BarcheDBGrid.SortMarker:= smUp;
  end;
end;

anonymous

2004-09-09 02:03

viewer   ~0005206

You cannot sort this kind of field. This is not a DBGrid limitation but an ADO one (try to sort with ORDER BY instead).

davalv

2004-09-09 07:15

reporter   ~0005208

I suggest to use this simple code to prevent exceptions:

//---------
  if Column.Field.FieldKind = fkLookup then Exit;
//---------

anonymous

2004-09-09 08:32

viewer   ~0005210

I decided to put all the sorting stuff in JvDBUltimGrid (from my point of view, the sorting implemented in JvDBGrid is really crappy and will not work with most of my apps but I let it there for other users and write what I need in JvDBUltimGrid). In JvDBUltimGrid, I raise an exception when one tries to sort a lookup field (and all kinds of non-data fields) because it is not possible to sort this field with many DB engines. To prevent this exception from raising and warn your users, you can put the following code (C++) in the OnCheckButton event:

   if ((Field->DataType == ftMemo)
   || ((Field->FieldKind != fkData) && (Field->FieldKind != fkInternalCalc)))
   {
      Sysutils::Beep;
      Enabled = false;
   }

Fred

obones

2004-10-07 11:30

administrator   ~0005342

Please test the latest version of the JvDBUltimGrid.pas file from CVS. Fred provided a fix for this.

obones

2005-04-01 05:51

administrator   ~0006807

No feedback has been provided, this issue is assumed to be fixed.

Issue History

Date Modified Username Field Change
2004-08-26 02:43 davalv New Issue
2004-08-26 02:51 davalv Note Added: 0005095
2004-08-26 03:06 davalv Note Added: 0005096
2004-09-09 02:03 anonymous Note Added: 0005206
2004-09-09 07:15 davalv Note Added: 0005208
2004-09-09 08:32 anonymous Note Added: 0005210
2004-10-07 11:30 obones Note Added: 0005342
2004-10-07 11:30 obones Status new => feedback
2005-04-01 05:51 obones Status feedback => resolved
2005-04-01 05:51 obones Fixed in Version => 3.00
2005-04-01 05:51 obones Resolution open => fixed
2005-04-01 05:51 obones Assigned To => obones
2005-04-01 05:51 obones Note Added: 0006807