Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to control Grid column width?

Status
Not open for further replies.

delphiman

Programmer
Dec 13, 2001
422
ZA

From Delphi 4 onwards I have noticed that
setting column widths in a grid via the
Properties in the OI is unreliable. And
Delphi often (inexplicably) simply ignores it.

The following code solved the problem. (I thought.)

procedure TfrmMyForm.FormCreate(Sender: TObject);
begin
dbGrd.columns.items [0].Width:= 28;
dbGrd.columns.items [1].Width:= 36;
dbGrd.columns.items [2].Width:= 36;
end;

But am now using Delphi 6 and the above
produces the following exception.

'ListIndex out of bounds[1]'

Can someone please tell me what I am missing?

 
Maybe your dbgrid has only 2 columns, or it is not tied to a datasource Steven van Els
SAvanEls@cq-link.sr
 
HI Steven! Nice to hear from you again. :)

In fact I have 6 columns but I get the exception on the column [0]. And the grid is tied to a DataSource.

What do you think?
 
hi DelphiMan

You're probably getting the error because the dataset is closed and the grid defaults to 1 column.

Have you tried Right clicking on the grid and adding all the relevant fields via the Columns Editor(right click in the Editor and 'Add all Fields') then resizing them in there?

lou
 
It looks like MissLed got the answer. If the dataset is closed when the form is created you will receive this type of error. The form is autocreated? Steven van Els
SAvanEls@cq-link.sr
 
Lou.
>Have you tried Right clicking on the grid and adding all the relevant fields via the Columns Editor(right click in the Editor and 'Add all Fields') then resizing them in there?

Yes. That's the only way one can do it during DesignTime as far as I know. :)

svan els
>If the dataset is closed when the form is created you will receive this type of error.

But then I would also have a blank grid. Which doesn't happen. I can see the data but have to use the horizontal-scroll-bar to see all the fields.

 
This stupid problem is getting in the way!! :-(

Anyone else have any ideas on it?

PUHlease!! :)
 
Fixed and case closed.

Moral of the story is as follows:

If one changes the size of a field it seems
that one MUST:

1.
REPLACE the original grid.
2.
Delete one's DataSet and Table
Components and start again. With fresh ones.

It seems one even has to come out of Delphi
AND even switch off one's system. And switch
it on again.

Perhaps adjusting one's chair height (and
opening and closing the door behind one and
making a fresh cup of coffee) also has
something to do with it! :)

Because that's all I did! :) :)

I can't begin to imagine the proper technical
explanation.
 
DelphiMan

>But then I would also have a blank grid. Which doesn't happen. I can see the data but have to use the horizontal-scroll-bar to see all the fields.

I just want to add...and one apologises if you know already:
IF one has added the appropriate fields via the query's Fields Editor (different to the cols editor!), one doesn't have to make the query active to be able to add the fields to the DBGrid via the Columns Editor.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top