Solved

Dislaying database information

Posted on 2004-09-05
10
204 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
  • 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
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 
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

Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

Suggested Solutions

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

778 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