Page 1 of 1

Dynamically set dgColumnWidth

Posted: Tue Dec 16, 2014 10:57 pm
by glenn52
I have a data grid on a tab control
The data grid is filled from a delimited text file
This file can contain different data (rows) and different column names
How can I dynamically adjust column widths to suit variable column name widths

ALSO

How can I lock column widths set using the column separator, for the current session
As of now I can use the column separator to set widths but when I go to another tab & return, the column widths have reset to default widths.

:D

Re: Dynamically set dgColumnWidth

Posted: Wed Dec 17, 2014 1:07 am
by dave.kilroy
Hi

First of all, in the data you are using to populate your datagrid (dgData or dgText), ascertain the max length of the strings that will occupy each column - and then set the width of relevant columns with something like the following for each column:

Code: Select all

set the dgColumnWidth["columnName"] of grp "DataGrid1" to tNecessaryWidth
 
As regards 'locking' column widths - I haven't experienced them resetting as you describe, unless you have other code somewhere else in your app changing them, using 'dgColumnWidth' should do the trick for you

Re: Dynamically set dgColumnWidth

Posted: Wed Dec 17, 2014 1:24 am
by glenn52
I had considered looping through each column header to ascertain length.
I also considered a similar process for cell data since typically this is wider than the header.
This all seemed awkward and was wondering if there was something a little more elegant.

The column widths reset, and there is no other code to initiate that. :cry:

Re: Dynamically set dgColumnWidth

Posted: Wed Dec 17, 2014 1:37 am
by dave.kilroy
If you are repopulating your datagrid as you enter the 'tab' then I would expect it it display with default widths each time (and would need to reset the dgColumnWidth each time) - and yes - I would indeed work with the length of the text that appears in the columns rather than the column headers (maybe some way to record text length as you build dgText/dgData?)

As regards increased elegance, that's how I do it (maybe don't repopulate if you don't need to) but why not ask Zyrup the Slug or Trevor? (Zyrup or Trevor - can you add to this thread?)

Kind regards

Dave