Solved

Dislaying database information

Posted on 2004-09-05
10
208 Views
Last Modified: 2010-04-05
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
Comment
Question by:wildzero
[X]
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
  • 5
  • 5
10 Comments
 
LVL 12

Expert Comment

by:esoftbg
ID: 11983681
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
 
LVL 10

Author Comment

by:wildzero
ID: 11985349
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
 
LVL 10

Author Comment

by:wildzero
ID: 11985352
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 12

Expert Comment

by:esoftbg
ID: 11985769
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
 
LVL 10

Author Comment

by:wildzero
ID: 11986414
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
 
LVL 12

Expert Comment

by:esoftbg
ID: 11987268
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
 
LVL 10

Author Comment

by:wildzero
ID: 11988610
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
 
LVL 12

Accepted Solution

by:
esoftbg earned 175 total points
ID: 11989861
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
 
LVL 10

Author Comment

by:wildzero
ID: 11994938
Hey there

Looks great, works perfect :D

Many thanks!
0
 
LVL 12

Expert Comment

by:esoftbg
ID: 11995181
You are welcome !
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

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…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

733 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