?
Solved

Question about the dbgrid.

Posted on 2001-08-09
16
Medium Priority
?
400 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
[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
  • 7
  • 6
  • 3
16 Comments
 
LVL 21

Accepted Solution

by:
gemarti earned 200 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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
 
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

Technology Partners: 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

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
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 this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Suggested Courses
Course of the Month14 days, 1 hour left to enroll

800 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