SupWang
asked on
Disable/Enable field's column of dbgrid flexibly?
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
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
ASKER
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
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Hi ITugay,
Can I use a name instead of "DBGrid1.Columns[I]"?
Regards, supwang
Can I use a name instead of "DBGrid1.Columns[I]"?
Regards, supwang
ASKER
I mean that like the Query1.FieldByName('YourFi eld') can instead of Query1.Field[1],
What can instead of "DBGrid1.Columns[I]"?
What can instead of "DBGrid1.Columns[I]"?
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.FieldNam e = 'FName1') or (Columns[I].Field.FieldNam e = 'FName2') then
if Columns[I].Field.AsString <> '' then
Columns[I].Visible := True;
Query1.Next;
end;
-----
Igor.
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.FieldNam
if Columns[I].Field.AsString <> '' then
Columns[I].Visible := True;
Query1.Next;
end;
-----
Igor.
ASKER
Hi ITugay,
The compiler show me a error message: "Undeclared identifier: 'Visable';"
I change "DBGrid1.Columns[I].Visibl e := 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?
The compiler show me a error message: "Undeclared identifier: 'Visable';"
I change "DBGrid1.Columns[I].Visibl
ASKER
Hi ITugay,
I found that can do "DbGrid1.Columns[0].Visibl e := 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
I found that can do "DbGrid1.Columns[0].Visibl
Regards, supwang
Hi,
not sure about D3, but seems you can set Columns[I].width = 0.
-----
Igor.
not sure about D3, but seems you can set Columns[I].width = 0.
-----
Igor.
ASKER
No. not very nice... more ideas?
may be just delete those columns?
ASKER
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?
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?
ok,
here is another version, but I'm not sure in TField.Visible in D3.
procedure TForm1.Query1AfterOpen(Dat aSet: 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;
here is another version, but I'm not sure in TField.Visible in D3.
procedure TForm1.Query1AfterOpen(Dat
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
end;
Query1.First;
Query1.EnableControls;
end;
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.
-----
Igor.
ASKER
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
---------->
Query1.FieldByName(Columns
----------<
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
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.
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.
ASKER
Hi,
Because I still using D3, so I can't use "DbGrid1.Columns[0].Visibl e := false;"
But I found when I don't do the following, then can use "Table1.FieldByName('TheFi eld').Visi ble := 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
-------------------------<
Because I still using D3, so I can't use "DbGrid1.Columns[0].Visibl
But I found when I don't do the following, then can use "Table1.FieldByName('TheFi
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
-------------------------<
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.
if you are don't going to change those properties, then can use Fields[I].Visible = False;
-----
Igor.
ASKER
Hi ITugay,
Because I still using D3, so I can't use "DbGrid1.Columns[0].Visibl e := false;"
And
1.If I add the fields in Columns Editor, then I can't use "Table1.FieldByName('TheFi eld').Visi ble :=
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
Because I still using D3, so I can't use "DbGrid1.Columns[0].Visibl
And
1.If I add the fields in Columns Editor, then I can't use "Table1.FieldByName('TheFi
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
Query1.FieldByName('YourFi