Solved

Disable/Enable field's column of dbgrid flexibly?

Posted on 2001-08-03
19
978 Views
Last Modified: 2010-04-06
Hi all,
I have a TQuery, and show its records in a dbgrid.

Some fields maybe not have value in all records.

I want to do
-------------------------->
if   "this field not have value in all records"   then   "don't show this field's column of dbgrid"  .
--------------------------<

The DB have many records, What's is the fastest way to do that?

Regards, supwang
0
Comment
Question by:SupWang
  • 10
  • 8
19 Comments
 
LVL 7

Expert Comment

by:Motaz
ID: 6350974
To hide a record from DBGrid:

Query1.FieldByName('YourField').Visible:= False;
0
 
LVL 2

Author Comment

by:SupWang
ID: 6351156
Hi Motaz,
No. Four fields of this TQuery may not have value, when them not have value, I want to hide its column of dbgrid(Save the display space, because have many fields).

And Do I must use a "for" loop to check all the record?

Regards, supwang
0
 
LVL 9

Accepted Solution

by:
ITugay earned 50 total points
ID: 6351243
Hi SupWang,

write this procedure to handle TQuery.AfterOpen event.

procedure TForm1.Query1AfterOpen(DataSet: TDataSet);
var
  I: Integer;
begin
  Query1.DisableControls;
  for I := 0 to DBGrid1.Columns.Count-1 do
    DBGrid1.Columns[I].Visible := False;
  while not Query1.EOF do
  begin
    for I := 0 to DBGrid1.Columns.Count-1 do
      if DBGrid1.Columns[I].Field.AsString <> '' then
        DBGrid1.Columns[I].Visible := True;
    Query1.Next;
  end;
  Query1.First;
  Query1.EnableControls;
end;

-----
Igor
0
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 
LVL 2

Author Comment

by:SupWang
ID: 6362928
Hi ITugay,
Can I use a name instead of "DBGrid1.Columns[I]"?

Regards, supwang
0
 
LVL 2

Author Comment

by:SupWang
ID: 6362943
I mean that like the Query1.FieldByName('YourField') can instead of Query1.Field[1],  
What can instead of "DBGrid1.Columns[I]"?
0
 
LVL 9

Expert Comment

by:ITugay
ID: 6363741
Yes, you can.
Anyway you should make associated column of DBGrid invisible. It more difficult then simply to work with columns. If you are going to check not all fields, then write some condition:

                      while not Query1.EOF do
                      with DBGrid1 do      
                      begin
                        for I := 0 to Columns.Count-1 do
                        // here is condition to check only FName1 and FName2 fields
                        if  (Columns[I].Field.FieldName = 'FName1') or (Columns[I].Field.FieldName = 'FName2') then

                          if Columns[I].Field.AsString <> '' then
                            Columns[I].Visible := True;
                        Query1.Next;
                      end;

-----
Igor.
0
 
LVL 2

Author Comment

by:SupWang
ID: 6368636
Hi ITugay,
The compiler show me a error message: "Undeclared identifier: 'Visable';"

I change "DBGrid1.Columns[I].Visible := False;" to "DBGrid1.Columns[I].Field.Visible := False;" then the code can be compiled. But I want to make the Columns invisible. How to do?
0
 
LVL 2

Author Comment

by:SupWang
ID: 6371223
Hi ITugay,
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 9

Expert Comment

by:ITugay
ID: 6371700
Hi,

not sure about D3, but seems you can set Columns[I].width = 0.

-----
Igor.
0
 
LVL 2

Author Comment

by:SupWang
ID: 6371747
No. not very nice...  more ideas?
0
 
LVL 9

Expert Comment

by:ITugay
ID: 6371776
may be just delete those columns?
0
 
LVL 2

Author Comment

by:SupWang
ID: 6371798
Hi ITugay,
What Delphi version are you using? I use the delphi4 to compile my project(the exe is 1.7M, have two thread), the program change to very slowly. But Delphi 3 is very fine. How about Delphi5? Can it better than D4?
0
 
LVL 9

Expert Comment

by:ITugay
ID: 6371805
ok,
here is another version, but I'm not sure in TField.Visible in D3.

procedure TForm1.Query1AfterOpen(DataSet: TDataSet);
var
 I: Integer;
 S: set of byte;
begin
 Query1.DisableControls;
 S := [];

 with DBGrid1 do
 begin
   while not Query1.EOF do
   begin
     for I := 0 to Columns.Count-1 do
       if Columns[I].Field.AsString <> '' then
         S := S + [I];
     Query1.Next;
   end;

   for I := 0 to Columns.Count-1 do
     if not (I in S) then
        Query1.FieldByName(Columns[I].Field.FieldName).Visible := False;
 end;

 Query1.First;
 Query1.EnableControls;
end;
0
 
LVL 9

Expert Comment

by:ITugay
ID: 6371820
I'm using D5. It's more powerfull and convient then D4 of course. Also it has components to acces IB server directly, without BDE. It's important for me. And I always prefer to use last compilers, but only after half of year testing by another programmers. So, I still don't use D6 (waiting for bugs:-)
-----
Igor.
0
 
LVL 2

Author Comment

by:SupWang
ID: 6371842
About
---------->
Query1.FieldByName(Columns[I].Field.FieldName).Visible := False;
----------<
It seems it can't hide the Columns of DBGrid.

How about the program?s speed which compiled by D5? Do you notices this point between D3/D4/D5?

Thanks, supwang
0
 
LVL 9

Expert Comment

by:ITugay
ID: 6371879
strange, it works fine for me.... may be regarding difference in Delphi versions.

another idea, what about to to move empty fieds to the right end of the grid?

The compilling speed looks like the same for D5 and D4 (don't remember for D3, used it long time ago).
Anyway, I'm strongly recommend to use D5 :-)
It's really good tool to build an applications and it has two update packages, it's mean that all (may be not all but I never meet it) bugs removed.

-----
Igor.
0
 
LVL 2

Author Comment

by:SupWang
ID: 6372037
Hi,
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 9

Expert Comment

by:ITugay
ID: 6372129
The objective of usage is simple. You can adjust some fields properties e.g. "Alignment", "Color", "DisplayName" etc.

if you are don't going to change those properties, then can use Fields[I].Visible = False;

-----
Igor.
0
 
LVL 2

Author Comment

by:SupWang
ID: 6378291
Hi ITugay,
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 is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Suggested Solutions

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
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…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

810 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