Mega Code Archive

 
Categories / Delphi / ADO Database
 

Simplest way to display dataset record on the fly

Title: Simplest way to display dataset record on the fly Question: Did you ever tried to display current DataSet record on the fly using surface of any kind of TWinControl? Answer: There are many possibilities for that. The following solution does just that in a simplest way. All what you have to do is add unit to project and implement procedures Do_TStringGrid and Display_Data every time you want to display data in current record of TDataSet. unit DataGrid; interface uses Windows, Controls, Grids, DB, Graphics; Type TDataGr = class private procedure FStringGridDrawCell(Sender: TObject; ACol, ARow: Integer; Rect: TRect; State: TGridDrawState); end; // Create TStringGrid with current record of ADSet Procedure Do_TStringGrid(AParent : TWinControl; ADSet : TDataSet); // Display data Procedure Display_Data(AParent : TWinControl; ADSet : TDataSet); implementation Var DataGr : TDataGr; // Display data Procedure Display_Data(AParent : TWinControl; ADSet : TDataSet); Var kCol, kRow, k : Integer; begin if AParent.ControlCount = 0 then Exit; kCol := 0; kRow := -1; with TStringGrid(AParent.Controls[0]) do for k:=0 to ADSet.FieldList.Count-1 do begin Inc(kRow); Cells[kCol, kRow] := ADSet.FieldList[k].FieldName; Cells[kCol+1, kRow] := ADSet.FieldByName(ADSet.FieldList[k].FieldName).AsString; if kRow = RowCount-1 then begin kRow := -1; Inc(kCol, 2); end; end; end; // Create TStringGrid with current record of ADSet Procedure Do_TStringGrid(AParent : TWinControl; ADSet : TDataSet); Var k : Integer; begin while AParent.ControlCount 0 do AParent.Controls[0].Free; with TStringGrid.Create(AParent.Owner) do begin Parent := AParent; DefaultRowHeight := 16; k := (Parent.ClientHeight div (DefaultRowHeight + GridLineWidth)) - 1; RowCount := k; Align := alClient; FixedCols := 0; FixedRows := 0; ColCount := 2*((ADSet.FieldList.Count div RowCount)+1); DefaultColWidth := ClientWidth div ColCount - GridLineWidth; OnDrawCell := DataGr.FStringGridDrawCell; end; // Display data Display_Data(AParent, ADSet); end; procedure TDataGr.FStringGridDrawCell(Sender: TObject; ACol, ARow: Integer; Rect: TRect; State: TGridDrawState); begin if not Odd(ACol) then Exit; with TStringGrid(Sender) do begin Canvas.FillRect(Rect); if Cells[ACol, ARow] = '' then Canvas.Font.Style := [] else Canvas.Font.Style := [fsBold]; Canvas.TextOut(Rect.Left, Rect.Top, Cells[ACol, ARow]); end; end; end. Usage: ... Do_TStringGrid(Panel1, Query1); to track scrolling data put following line in the AfterScroll event of the TDataSet ... Display_Data(Panel1, Query1) I hope this tip can be useful to Database programmers. NOTES: 1. It is assumed that AParent doesn't have any controls on it. Otherwise one must search previously created StringGrid in any other manner to destroy it when it is needed repetedly use procedure Do_TStringGrid. If so replace lines while AParent.ControlCount 0 do AParent.Controls[0].Free; as needed. 2. Second asumption is that all field values can be displyed as strings. If there are blob fields (i.e. graphics) in the DataSet one must improve the solution. But these cases seems to be too rarely encountered.