Recent

Author Topic: error at any code of dbgrid sort  (Read 8259 times)

xenblaise

  • Sr. Member
  • ****
  • Posts: 358
error at any code of dbgrid sort
« on: October 15, 2010, 08:24:03 am »
>>this code;(error) same, when I scroll my mouse at the dbgrid
procedure TForm1.DBGrid1TitleClick(Column: TColumn);
begin
  (Column.Field.DataSet as TSQLQuery).IndexFieldNames:=Column.FieldName;
end;


>>this code;(error) same, when I scroll my mouse at the dbgrid
function TForm1.DBGridToggleSort(AFieldName: String; dsGrid: TSQLQuery): boolean;
var
  ix: TIndexDef;
begin
 //Reverse column sorting with each click
 if dsGrid.IndexFieldNames <> '' then
 begin
   //Sort Descending
   dsGrid.IndexFieldNames := '';
   ix := TIndexDef.Create(dsGrid.IndexDefs, AFieldName, AFieldName, [ixDescending]);
   dsGrid.IndexName := ix.Name;
 end
 else
 begin
   //Sort Ascending
   dsGrid.IndexName := '';
   dsGrid.IndexFieldNames := AFieldName;
 end;
end;

procedure TForm1.DBGridmLeaderTitleClick(Column: TColumn);
var
  dsGrid: TSQLQuery;
  AFieldName: string;
begin
  dsGrid := (Column.Field.DataSet as TSQLQuery);
  AFieldName := Column.FieldName;
  DBGridToggleSort(AFieldName, dsGrid);
end;

Whats on Lazarus dbgrid mouse scroll? it is the mouse scroll? or somehing else, what?

I am applying master and detail application.

The code:( I know the problem is not on the code below, but something when I scroll the mouse inside dbgrid)
procedure TForm1.FormShow(Sender: TObject);
begin

  SQLite3Connection1.Directory := '';
  SQLite3Connection1.DatabaseName := 'candid.db';
  SQLTransaction1.DataBase := SQLite3Connection1;
  SQLTransaction1.Action := caCommit;

  SQLTransaction1.Active := True;
  SQLite3Connection1.Transaction := SQLTransaction1;
  SQLite3Connection1.Connected := True;
  SQLQuery1.DataBase := SQLite3Connection1;
  SQLQuery1.SQL.Text := 'select * from leaders';
  SQLQuery1.Transaction := SQLTransaction1;
  Datasource1.DataSet := SQLQuery1;
  DbEdit1.DataField  := 'LEADER_ALIAS_NAME';
  DbEdit2.DataField  := 'PRECINCT_NUMBER';
  DbMemo1.DataField  := 'LEADER_NOTE';
  DbEdit1.DataSource := Datasource1;
  DbEdit2.DataSource := Datasource1;
  DbMemo1.DataSource := Datasource1;
  DBGridLeader.DataSource := Datasource1;
  DBGridmLeader.DataSource := Datasource1;
  DBGridPrint.DataSource := Datasource1;
  SQLQuery1.Open;


  SQLQuery2.DataBase := SQLite3Connection1;
  SQLQuery2.DataSource := Datasource1;
  SQLQuery2.Transaction := SQLTransaction1;
  SQLQuery2.SQL.Text := 'select MEMBER_NAME from MEMBERS where MEMBER_LEADERS_NAME = :LEADER_ALIAS_NAME';
  Datasource2.DataSet := SQLQuery2;
  DBGridMember.DataSource := Datasource2;
  DBGridPrintMember.DataSource := Datasource2;
  SQLQuery2.Open;


  SQLQuery3.DataBase := SQLite3Connection1;
  SQLQuery3.Transaction := SQLTransaction1;
  SQLQuery3.SQL.Text := 'select * from MEMBERS';
  Datasource3.DataSet := SQLQuery3;
  DBGridmMember.DataSource := Datasource3;
  DbEdit3.DataField  := 'MEMBER_NAME';
  DbEdit4.DataField  := 'MEMBER_LEADERS_NAME';
  DbMemo2.DataField  := 'MEMBER_NOTE';
  DbEdit3.DataSource := Datasource3;
  DbEdit4.DataSource := Datasource3;
  DbMemo2.DataSource := Datasource3;
  SQLQuery3.Open;

end;

xenblaise

  • Sr. Member
  • ****
  • Posts: 358
Re: error at any code of dbgrid sort
« Reply #1 on: October 15, 2010, 03:55:13 pm »
error is:
List Index (2) out of bounds.

xenblaise

  • Sr. Member
  • ****
  • Posts: 358
Re: error at any code of dbgrid sort
« Reply #2 on: October 16, 2010, 12:54:42 am »
Even this code errors;

procedure TForm1.DBGridMemberTitleClick(Column: TColumn);
begin
  if Column.Field.DataSet is TSQLQuery then
    TSQLQuery(Column.Field.DataSet).IndexFieldNames := Column.Field.FieldName
  else if Column.Field.DataSet is TSQLQuery then
  begin
    if TSQLQuery(Column.Field.DataSet).IndexDefs.GetIndexForFields(Column.Field.FieldName, False) = nil then Exit;
    TSQLQuery(Column.Field.DataSet).IndexFieldNames := Column.Field.FieldName;
  end;
end;

Any possible solution?

José Mejuto

  • Full Member
  • ***
  • Posts: 136
Re: error at any code of dbgrid sort
« Reply #3 on: October 16, 2010, 09:55:22 pm »
Even this code errors;
[...]
Any possible solution?

Hello,

That bug is fixed in fpc 2.5.1 at 99% but I think it also needs the small patch at the end of this bug report (about your problem):

http://bugs.freepascal.org/view.php?id=15460

I'm using 2.5.1 with the last patch in that bug report and it works at least for me.

xenblaise

  • Sr. Member
  • ****
  • Posts: 358
Re: error at any code of dbgrid sort
« Reply #4 on: October 17, 2010, 04:09:03 am »
Actually its not a bug, its my programming code error.
It does NOT affect if tables are NOT crosslink.
Im using Master detail database procedure.
Dbgrid1 uses 'leaders' table
Dbgrid2 uses 'members' table that is linked to Dbgrid1.

So every time I scroll Dbgrid1?, a Dbgrid2 data will appear.

And after a time when I click the columntitle from Dbgrid1 to Dbgrid2 back to Dbgrid1?, it errors.

List Index (2) out of bounds.

I just need the exact code to do the match.


Thank you for your comment.

 

TinyPortal © 2005-2018