revUp - Updates and news for the LiveCode community
Issue 117 | September 2nd 2011 Contact the Editor | How to Contribute

Change the Number Format of a Column by Script
Follow this step by step lesson helping you become expert in the use of datagrids

by Zyrip the Slug

DataGrid Helper is a popular Marketplace addon for LiveCode, which has recently been updated.

This lesson describes, how, once a datagrid is installed in a project, developers can continue changing the number format of a specific column without DGH.

This allows for example, the creation of a preferences dialog where the final user can set its format.

What Are the Custom Properties Added by DGH for Formatting Columns?

DGH adds a special custom property named dgh_FormatColumn in the datagrid group.

This custom property contains an array of two dimensions:
1. the first dimension is a column name.
2. the second dimension is the name of one of the three following format properties:
    - pattern
    - thousand sep
    - dec sep

How Do I Can Get The Column Number Format?

To read the format applied to a column, you can use the following code:

function readColumnFormat pTheDatagridName, pTheColumnName
    local tData, tThePattern, tTheThousandSep, tTheDecSep
    
    put the dgh_FormatColumn of group pTheDatagridName into tData
    put tData[pTheColumnName]["pattern"] into tThePattern
    put tData[pTheColumnName]["thousand sep"] into tTheThousandSep
    put tData[pTheColumnName]["dec sep"] into tTheDecSep
    
    return tThePattern & cr & tTheThousandSep & cr & tTheDecSep
end readColumnFormat

Where:
pTheDatagridName is the datagrid name
pTheColumnName is a column name

 

Example:
put readColumnFormat("datagrid 1", "numbers")

How Do I Can Set The Column Number Format?

To apply a number format to a column, you can use the following code:

command writeColumnFormat pTheDatagridName, pTheColumnName, pThePattern, pTheThousandSep, pTheDecSep
    local tData
    
    put the dgh_FormatColumn of group pTheDatagridName into tData
    put pThePattern into tData[pTheColumnName]["pattern"]
    put pTheThousandSep into tData[pTheColumnName]["thousand sep"]
    put pTheDecSep into tData[pTheColumnName]["dec sep"]
    set the dgh_FormatColumn of group pTheDatagridName to tData
    
    send "refreshList" to group pTheDatagridName
end writeColumnFormat

Where:
pTheDatagridName is the datagrid name
pTheColumnName is a column name
pThePattern is a pattern
pTheThousandSep is the thousands separator
pTheDecSep is the decimal separator

 

Example:
writeColumnFormat "datagrid 1", "numbers", "$### ##0[green],$### ##0[red]",",","."

Michael McCreary

About the Author

Zyrip is a long time xTalk user. By day, he is a developer in a small firm creating applications or websites for clients in languages such as Visual Basic, VB.Net, PHP and Fourth Dimension. By night, he develops with LiveCode, his favorite development environment.

 

Main Menu

What's New

Sign up for the September Academy