Solved

Question about the dbgrid.

Posted on 2001-08-09
16
388 Views
Last Modified: 2012-05-04
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
Comment
Question by:SupWang
  • 7
  • 6
  • 3
16 Comments
 
LVL 21

Accepted Solution

by:
gemarti earned 50 total points
ID: 6370908
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
 
LVL 2

Author Comment

by:SupWang
ID: 6371010
Hi gemarti,
The compiler show me a error message: "Undeclared identifier: 'Visable';"
0
 
LVL 21

Expert Comment

by:gemarti
ID: 6371020
the identifier is VISIBLE not VISABLE.

Check spelling in your code.

GEM :]
0
 
LVL 2

Author Comment

by:SupWang
ID: 6371046
oh, yes, but I dont had wrong. because I copied your code...  The compiler show me a error message: "Undeclared identifier: 'Visible' ;"
0
 
LVL 21

Expert Comment

by:gemarti
ID: 6371104
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
 
LVL 44

Expert Comment

by:CrazyOne
ID: 6371106
Try this

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


The Crazy One
0
 
LVL 21

Expert Comment

by:gemarti
ID: 6371109
OOps this line should be right after Table1.Active := true;


{Assign table one to dbgrid}
DBGrid1.DataSource := DataSource1;
0
 
LVL 44

Expert Comment

by:CrazyOne
ID: 6371115
Sorry SupWang I did not look at the other link. :>)
0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 44

Expert Comment

by:CrazyOne
ID: 6371139
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
 
LVL 2

Author Comment

by:SupWang
ID: 6371220
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
 
LVL 2

Author Comment

by:SupWang
ID: 6372034
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
 
LVL 21

Expert Comment

by:gemarti
ID: 6372080
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
 
LVL 2

Author Comment

by:SupWang
ID: 6372091
But I want to customize the line color of the DBGrid(use AccGridDrawColumnCell event)....    ???
0
 
LVL 21

Expert Comment

by:gemarti
ID: 6372117
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
 
LVL 2

Author Comment

by:SupWang
ID: 6372138
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
 
LVL 2

Author Comment

by:SupWang
ID: 6378301
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

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

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…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

706 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

20 Experts available now in Live!

Get 1:1 Help Now