• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 405
  • Last Modified:

Question about the dbgrid.

Hi all,
How can I disable(hide) a column of dbgrid by program?

and please visit:
http://www.experts-exchange.com/jsp/qManageQuestion.jsp?ta=delphi&qid=20163180

Regards, supwang
0
SupWang
Asked:
SupWang
  • 7
  • 6
  • 3
1 Solution
 
gemartiCommented:
Right Click on the Grid and Select Columns Editor
When Editor Appears right click on the box and select Add all Fields (or just the fields you want)

Now in your program to programatically hide a field at run time you just put this command in:


  DbGrid1.Columns[0].Visible := false;

Where 0 is an index value of the field you want to hide either temporarily or permenantly (Depending on how you are using the grid....
0
 
SupWangAuthor Commented:
Hi gemarti,
The compiler show me a error message: "Undeclared identifier: 'Visable';"
0
 
gemartiCommented:
the identifier is VISIBLE not VISABLE.

Check spelling in your code.

GEM :]
0
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!

 
SupWangAuthor Commented:
oh, yes, but I dont had wrong. because I copied your code...  The compiler show me a error message: "Undeclared identifier: 'Visible' ;"
0
 
gemartiCommented:
Okay do this: (If you accepted the default Delphi installation this code works}

Create a new application.

Place a

DataSource component
Table component
Database component and a
DBGrid component on the Form.

Paste this code in the unit file compile and run

----------------cut below line------------------

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, Grids, DBGrids, DB, DBTables;

type
  TForm1 = class(TForm)
    DataSource1: TDataSource;
    Table1: TTable;
    Database1: TDatabase;
    DBGrid1: TDBGrid;
    procedure FormActivate(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.FormActivate(Sender: TObject);
begin
{Set properties to database1}
Database1.AliasName := 'DBDEMOS';
Database1.LoginPrompt := false;
Database1.DatabaseName := 'DB';

{Set Properties to Table1}
Table1.DatabaseName := Database1.DatabaseName; {or DB}
Table1.TableName := 'clients.dbf';

{Set Properties to Datasource1}
DataSource1.DataSet := Table1;

{Set database connected:= true}
Database1.Connected := true;

{Set table1 active := true}
Table1.Active := true;

{Hide Last name from the clients.dbf file}
DbGrid1.Columns[0].Visible := false;
end;

end.

---------------cut above the line -------------


0
 
CrazyOneCommented:
Try this

  Table1.FieldByName('TheField').Visible := False;


The Crazy One
0
 
gemartiCommented:
OOps this line should be right after Table1.Active := true;


{Assign table one to dbgrid}
DBGrid1.DataSource := DataSource1;
0
 
CrazyOneCommented:
Sorry SupWang I did not look at the other link. :>)
0
 
CrazyOneCommented:
Does this look like something you are wanting


  Screen.Cursor := crHourGlass;
  Query1.DisableControls;
  repeat
    for i := 0 to Query1.FieldCount - 1 do
      if Trim(Query1.Fields[i].AsString) = '' then
        if Query1.Fields[i].Visible then
          Query1.Fields[i].Visible := False;
    Query1.Next;
  until Query1.Eof;
  Query1.EnableControls;
  Screen.Cursor := crDefault;
  ShowMessage('Finished');
0
 
SupWangAuthor Commented:
Hi gemarti and CrazyOne,
I found that can do "DbGrid1.Columns[0].Visible := false;" in Delphi4, but can't do that in Delphi3.  :-(  But I must use D3. How to do? and It seems that the "RxDBGrid for D3" also can't do that.

Regards, supwang
0
 
SupWangAuthor Commented:
Hi gemarti and CrazyOne,
Because I still using D3, so I can't use "DbGrid1.Columns[0].Visible := false;"

But I found when I don't do the following, then can use "Table1.FieldByName('TheField').Visible := False;".
Please tell me the usage about the following, when I don't add the fields, the dbgrid still work. so why need to add the fields?

------------------------->
Right Click on the Grid and Select Columns Editor
When Editor Appears right click on the box and select Add all Fields
-------------------------<
0
 
gemartiCommented:
Using the Columns Editor allows you to customize the output of the DBGrid. You can set column properties, such as color, font, width, alignment...for each field and title properties such as caption, font, and colors. Advanced properties you can use with the columns editor are ButtonStyle and DropDownRows or even PickLists (Dropdown Lists).

If Table1.FieldByName('TheField').Visible := False works for you then use it.

It would have been nice to know that you were working with Delphi 3 though.

Good Luck.

0
 
SupWangAuthor Commented:
But I want to customize the line color of the DBGrid(use AccGridDrawColumnCell event)....    ???
0
 
gemartiCommented:
What is stopping you?

I think your going to have to make some decisions. You have already decided that you have to use Delphi 3 or someone or something has made that decision for you; so you have to decide:

1. Do I use a table or query component.
2. Do I want to use the columns editor or don't I want to use it.
3. Do I use itugay's code, crazy one's code, or any other experts code that has contributed to this question and your previous question.
4. Is the invisibility of this column so vital that I have to sacrafice one piece of functionality for another.

All I can say is make a decision and live with it.

Good Luck.

GEM :->
0
 
SupWangAuthor Commented:
Hi gemarti,
1.If I add the fields in Columns Editor, then I can't use "Table1.FieldByName('TheField').Visible := False;".
2.If I don't add the fields in Columns Editor, then I can't customize the line color of the DBGrid(use AccGridDrawColumnCell event)......

How to do?

Regards, supwang
0
 
SupWangAuthor Commented:
Hi gemarti,
Because I still using D3, so I can't use "DbGrid1.Columns[0].Visible := false;"

And
1.If I add the fields in Columns Editor, then I can't use "Table1.FieldByName('TheField').Visible := False;".
2.If I don't add the fields in Columns Editor, then I can't customize the line color of the DBGrid(use AccGridDrawColumnCell event)......

so I will keep your codes for the future.

Thank you very much.

Regards, supwang
0

Featured Post

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!

  • 7
  • 6
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now