Page 1 of 1

Setting automatically column width of datagrid... any tip ?

Posted: Sun Aug 09, 2009 11:32 am
by titobal
Hi,

I would like to set automatically the columns widths of a datagrid
in function of the length of the texts/contents which are located in each cells,
any tip on how to perform that ?

Thanks

Posted: Wed Aug 12, 2009 4:02 pm
by trevordevore
You will have to do this by hand. You could do something like this to calculate the widest text in each column and update the column widths in the data grid. Note that the code has not been tested and you must have a field named "FigureOutWidth" on the card that contains this script.

Code: Select all

local theColWidthsA ## when all done has a key for each column name. Value of key is max width.

put the dgData of group "DataGrid" into theDataA
put the dgIndexes of group "DataGrid" into theIndexes
put the dgProps["columns"] of group "DataGrid" into theColumns

## Loop through all records
repeat for each item theIndex in theIndexes
    ## Loop through each column of record
    repeat for each line theColumn in theColumns
        ## Figure out width of this record's column
        set the text of field "FigureOutWidth" to theDataA[theIndex][theColumn]
        put max(the formattedwidth of field "FigureOutWidth", theColWidthsA[theColumn]) into theColWidthsA[theColumn]
    end repeat
end repeat

## Reset field
set the text of field "FigureOutWidth" to empty

## Set column widths
repeat for each line theColumn in theColumns
    put theColWidthsA[theColumn] & comma after theWidths
end repeat
delete the last char of theWidths
set the dgProps["column widths"] of group "DataGrid" to theWidths

## output max widths for testing purposes
repeat for each line theColumn in theColumns
    put "max width for column" && theColumn & ":" && theColWidthsA[theColumn] & cr after theText
end repeat

put theText

Amazing ! Many Thanks !

Posted: Wed Aug 12, 2009 9:37 pm
by titobal
Amazing ! Many Thanks !

Re: Setting automatically column width of datagrid... any tip ?

Posted: Tue Aug 09, 2011 1:48 am
by BarrySumpter
Nice!

Re: Setting automatically column width of datagrid... any ti

Posted: Mon Dec 16, 2013 6:22 am
by LC4iOS
Lovely.
Was just looking thru the pop library demo and have added a DataGrid following my first data grid sample lesson from RunRev.
Thanks RR

And wanted something just like this.
Thanks for posting that routine Trevor

But I don't know why the instructions are so hard to read for me. :oops:


Place a DataGrid control on a card.

Place a field named fldFigureOutWidth on that same card.

Place a routine named routineFigureOutWidth within that same cards Card Script.

Copy and paste the code above into the routine named routineFigureOutWidth.

Re: Setting automatically column width of datagrid... any ti

Posted: Mon Dec 16, 2013 1:54 pm
by Klaus
Quick question: Do you also EXECUTE your "routineFigureOutWidth" handler at any time? 8)

Re: Setting automatically column width of datagrid... any ti

Posted: Mon Dec 16, 2013 9:47 pm
by LC4iOS
Only if you want it to work.
:mrgreen:

Re: Setting automatically column width of datagrid... any ti

Posted: Mon Dec 16, 2013 10:20 pm
by Klaus
Ah, sorry, I had the expression that it did not work somehow 8)

Re: Setting automatically column width of datagrid... any ti

Posted: Tue Dec 17, 2013 10:34 am
by LC4iOS
Over the last few days I had read that line n kept getting confused.
Thought I had posted the script works perfectly.
Must have removed it from my numerous edits. :oops:

Was going to post a sample project
but got distracted trying to make a column color grey and a cell bold and red on the datagrid
on a simple table style (with no templates or custom behaviours).

The project is getting bit out of hand.
And needs a bit of clean up from my playing in me sandbox.

Project added.

Re: Setting automatically column width of datagrid... any ti

Posted: Sun Dec 22, 2013 10:03 am
by LC4iOS
Weird a Time headed column of hh:mm:ss format keeps getting squashed.

Updated project.
But not really happy with logic.

Maybe the header font is different than the data font?

Another update.
the field FigureOutWidth has to be dontWrap

Code: Select all


on mouseUp
   
   local theColWidthsA ## when all done has a key for each column name. Value of key is max width.
   
   put the dgData of group "DataGrid" into theDataA
   --put the dgText[True] of group "DataGrid" into theDataA
   put the dgIndexes of group "DataGrid" into theIndexes
   put the dgProps["columns"] of group "DataGrid" into theColumns
   
   
   
   
   ## Loop through all records
   repeat for each item theIndex in theIndexes
          ## Loop through each column of record
          repeat for each line theColumn in theColumns
               
           ## Figure out width of this record's column
         
         
         if  the dgColumnLabel[theColumn] of group "DataGrid"  is empty then
            set the text of field "FigureOutWidth" to theColumn
         else
            set the text of field "FigureOutWidth" to the dgColumnLabel[theColumn] of group "DataGrid"
         end if
         put the formattedwidth of field "FigureOutWidth"  onto theHeaderWidth
             
         set the text of field "FigureOutWidth" to theDataA[theIndex][theColumn]
         put the formattedwidth of field "FigureOutWidth" onto theCellWidth
         
         if theHeaderWidth > theCellWidth then
            put round(theHeaderWidth * 1.5) into theHeaderWidth
         end if
         
         
         
         if theColumn = "chk" then
            put 0 into theHeaderWidth
         end if
         
         
             put max(theHeaderWidth, theCellWidth, theColWidthsA[theColumn]) into theColWidthsA[theColumn]
                -- put max(the formattedwidth of field "FigureOutWidth", theColWidthsA[theColumn]) into theColWidthsA[theColumn]
          end repeat
   end repeat
   
   ## Reset field
   set the text of field "FigureOutWidth" to empty
   
   ## Set column widths
   repeat for each line theColumn in theColumns
          put theColWidthsA[theColumn] & comma after theWidths
   end repeat
   delete the last char of theWidths
   set the dgProps["column widths"] of group "DataGrid" to theWidths
   
   ## output max widths for testing purposes
   repeat for each line theColumn in theColumns
          put "max width for column" && theColumn & ":" && theColWidthsA[theColumn] & cr after theText
   end repeat
   
   put theText
   
end mouseUp