[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 216
  • Last Modified:

Dislaying database information

Using DBGrid or a component similar can you have it so instead of the headings at the top, they are on the left
Example

Name       Age
Sam         31
Dave        23

It would be

Name      Sam      Dave
Age         31         23

See what I mean?

Cheers
Nick
0
wildzero
Asked:
wildzero
  • 5
  • 5
1 Solution
 
esoftbgCommented:
download a working example from:
page:        http://www.geocities.com/esoftbg/
  link:        Q_21119543.zip

//................

procedure TForm1.SpeedButtonFieldsClick(Sender: TObject);
var
  I:             Integer;
  ListColumn:    TListColumn;
  ListItem:      TListItem;
begin
  ListView.Clear;
  ListView.Columns.Clear;
  ListColumn := ListView.Columns.Add;
  ListColumn.Caption := 'Field Name';
  ListColumn.Width := 72;
  ListColumn := ListView.Columns.Add;
  ListColumn.Caption := 'Name';
  ListColumn.Width := 204;
  ListColumn := ListView.Columns.Add;
  ListColumn.Caption := 'Age';
  ListColumn.Width := 68;
  tblEMPLOEE.First;
  while not tblEMPLOEE.EOF do
  try
    for I := 0 to tblEMPLOEE.FieldCount-1 do
    begin
      ListItem := ListView.Items.Add;
      ListItem.Caption := tblEMPLOEE.Fields[I].FullName;
      ListItem.SubItems.Add(tblEMPLOEE.FieldByName('Name').AsString);
      ListItem.SubItems.Add(tblEMPLOEE.FieldByName('Age').AsString);
    end;
  finally
    tblEMPLOEE.Next;
  end;
end;
0
 
wildzeroAuthor Commented:
Hey there,

Not quite what I am after, all that does is lists the values in a listview.... like
Name       Age
Sam         31
Dave        23

Where NAME and AGE and the headings
The headings need to be on the side like

Name      Sam      Dave
Age         31         23
0
 
wildzeroAuthor Commented:
Just to explain it a little better
there would be 1 row per record - just like DBGrid
and there wouldn't be headings at the top of the grid, just on the side.

Many thanks
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

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

 
esoftbgCommented:
download a fantastic solution from above page:

//........

procedure TForm1.SpeedButtonFieldsClick(Sender: TObject);
var
  I:             Integer;
  N:             Integer;
  ListColumn:    TListColumn;
  ListItem:      TListItem;
begin
  ListView.Clear;
  ListView.Columns.Clear;
  N := CountOfRecords('MTV');
  ListColumn := ListView.Columns.Add;
  ListColumn.Width := 64;
  for I := 1 to N do
  begin
    ListColumn := ListView.Columns.Add;
    ListView.Columns[I].Caption := IntToStr(I);
    ListColumn.Width := 96;
  end;

  ListItem := ListView.Items.Add;
  ListItem.Caption := 'Name';
  tblMTV.First;
  while not tblMTV.EOF do
  begin
    ListItem.SubItems.Add(tblMTV.FieldByName('Name').AsString);
    tblMTV.Next;
  end;
  ListItem := ListView.Items.Add;
  ListItem.Caption := 'Age';
  tblMTV.First;
  while not tblMTV.EOF do
  begin
    ListItem.SubItems.Add(tblMTV.FieldByName('Age').AsString);
    tblMTV.Next;
  end;
end;
0
 
wildzeroAuthor Commented:
Hey there,

Thanks for the reply.
That way just makes the first column in the listview the 'headings' right? Is there no way to make them look like the 'normal' headings? Like, normally they are up the top and are grey and not in 'cells'.True I could set the background color of those cells to grey but they don't quite look right.

Any suggestions?
0
 
esoftbgCommented:
Hi wildzero,
To hide the ColumnHeaders, just set:

  ListView.ShowColumnHeaders := False;  // by ObjectInspector

//  or by code:

procedure TForm1.FormCreate(Sender: TObject);
begin
  tblMTV.Active := True;
  ListView.ShowColumnHeaders := False;
end;

I think if you need it is possible to create a row before the real content: Name and Age. Just if you need it ....
0
 
wildzeroAuthor Commented:
Hey there :)

I have found a better way of doing it, instead of listview, using string grid!
That way you get the headers on the side by calling FixedRow := 0 and FixedCol := 0;
Also
StrGrid.Cells[0,0] := 'Name';
StrGrid.Cells[0,1] := 'Age'; and then loading them in by a loop.

Now I know you've done a bit of work on this for me.
So, if you could do this thing for me I will give you all the points :)

If you could write the loop to load the data into the grid, but make it so
If there are three 'lots of data' then it resizes the String grid to fit it so no scroll bars. If there is four 'lots of data' ( people) it resizes the string grid's width to suit. See what I mean?

And lastly, for an extra 50 points.
If you could make it so a value changed in the grid changes in the database - that would be super excellent.
So if Sam changes his age in the string grid from 31 to 32, it updates that record in the database.

Hows that sound?

Many many thanks for you help.
Nick
0
 
esoftbgCommented:
Download the example again:

unit Unit1_Q_21119543;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, DB, DBTables, Grids, DBGrids, StdCtrls, Buttons, ComCtrls,
  ExtCtrls, JPEG;

type
  TForm1 = class(TForm)
    dstEMPLOEE: TDataSource;
    DBGridEMPLOEE: TDBGrid;
    SpeedButtonFields: TSpeedButton;
    tblMTV: TTable;
    QueryCount: TQuery;
    SpeedButton1: TSpeedButton;
    EditCount: TEdit;
    Image5: TImage;
    Image4: TImage;
    Image1: TImage;
    Image2: TImage;
    Image3: TImage;
    StringGrid: TStringGrid;
    procedure FormCreate(Sender: TObject);
    procedure SpeedButtonFieldsClick(Sender: TObject);
    procedure SpeedButton1Click(Sender: TObject);
    procedure StringGridSetEditText(Sender: TObject; ACol, ARow: Integer; const Value: String);
  private   { Private declarations }
    CanEdit:  Boolean;
  public    { Public declarations }
    function  CountOfRecords(TableName: string): Integer;
    procedure FillStringGrid;
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
begin
  CanEdit := False;
  tblMTV.Active := True;
  FillStringGrid;
end;

procedure TForm1.FillStringGrid;
var
  I:      Integer;
  N:      Integer;
begin
  CanEdit := False;
  try
    N := CountOfRecords('MTV');
    StringGrid.FixedRows := 0;
    StringGrid.RowCount := 2;
    StringGrid.ColCount := N+1;
    StringGrid.DefaultColWidth := 594 div (N+1);
    StringGrid.Cols[0].Capacity := 4;
    for I := 1 to N do
      StringGrid.Cols[I].Capacity := 32;
    StringGrid.Cells[0,0] := 'Name';
    StringGrid.Cells[0,1] := 'Age';
    tblMTV.First;
    I := 0;
    while not tblMTV.EOF do
    begin
      Inc(I);
      StringGrid.Cells[I, 0] := tblMTV.FieldByName('Name').AsString;
      StringGrid.Cells[I, 1] := tblMTV.FieldByName('Age').AsString;
      StringGrid.Cells[I, 2] := tblMTV.FieldByName('ID').AsString;
      tblMTV.Next;
    end;
  finally
    CanEdit := True;
  end;
end;

procedure TForm1.SpeedButtonFieldsClick(Sender: TObject);
begin
  FillStringGrid;
end;

function  TForm1.CountOfRecords(TableName: string): Integer;
var
  Count: Integer;
begin
  Count := 0;
  try
    QueryCount.Active := False;
    QueryCount.SQL.Text :=''
                        + 'SELECT COUNT(*) FROM '
                        + TableName
                        + '';
    QueryCount.Active := True;
    Count := QueryCount.Fields[0].AsInteger;
  finally
    Result := Count;
  end;
end;

procedure TForm1.SpeedButton1Click(Sender: TObject);
begin
  EditCount.Text := IntToStr(CountOfRecords('MTV'));
end;

procedure TForm1.StringGridSetEditText(Sender: TObject; ACol, ARow: Integer; const Value: String);
begin
  if CanEdit then
  begin
    if tblMTV.Locate('ID', StringGrid.Cells[ACol, 2], []) then
    begin
      tblMTV.Edit;
      case ARow of
        0: tblMTV.FieldByName('Name').AsString := StringGrid.Cells[ACol, 0];
        1: tblMTV.FieldByName('Age').AsString := StringGrid.Cells[ACol, 1];
      end;
      tblMTV.Post;
    end;
  end;
end;

end.
0
 
wildzeroAuthor Commented:
Hey there

Looks great, works perfect :D

Many thanks!
0
 
esoftbgCommented:
You are welcome !
0

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

  • 5
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now