Dynamically set dgColumnWidth

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
glenn52
Posts: 25
Joined: Mon May 02, 2011 1:34 pm
Contact:

Dynamically set dgColumnWidth

Post by glenn52 » Tue Dec 16, 2014 10:57 pm

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

dave.kilroy
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 858
Joined: Wed Jun 24, 2009 1:17 pm
Contact:

Re: Dynamically set dgColumnWidth

Post by dave.kilroy » Wed Dec 17, 2014 1:07 am

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
"...this is not the code you are looking for..."

glenn52
Posts: 25
Joined: Mon May 02, 2011 1:34 pm
Contact:

Re: Dynamically set dgColumnWidth

Post by glenn52 » Wed Dec 17, 2014 1:24 am

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:

dave.kilroy
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 858
Joined: Wed Jun 24, 2009 1:17 pm
Contact:

Re: Dynamically set dgColumnWidth

Post by dave.kilroy » Wed Dec 17, 2014 1:37 am

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
"...this is not the code you are looking for..."

Post Reply