Solved

Dislaying database information

Posted on 2004-09-05
10
201 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
 
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

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…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
This video discusses moving either the default database or any database to a new volume.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

708 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now