We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date
close icon

Formatting cell data on demand

For my datagrid, I have a ObservableCollection<Dictionary<string,object>> as my datasource where each column is mapped to a specific field in each dictionary.  Using the provided example at http://www.syncfusion.com/downloads/support/directtrac/175508/ze/175508-8059138  I was able to render all of the data in my grid.

The grid pulls the data directly from the datasource and displays it in a cell of the table, but is there a way to call a method to format that data after it is fetched but before it is displayed?

Basically, I need something like this to occur:

object dataFromDataSource = <whatever method would normally provide this to the grid cell using "Values[" + colProperty + "]" as the mapping>
string formattedData = formatExpression(dataFromDataSource);
return formattedData;//this is what should be displayed in the cell


3 Replies

VP Vimal Prabhu Manohkaran Syncfusion Team April 3, 2017 05:56 AM UTC

Hi Clint,

Thanks for using Syncfusion products.

We have analyzed your query. If you requirement is to set Format or culture info to the GridCell’s text, please refer the below UG link which explains on how to apply different formats to the GridCell’s text.

UG Link : https://help.syncfusion.com/xamarin-ios/sfdatagrid/column-types#formatting

If in case your requirement is to change the text color, font, size etc of the GridCell’s text based on the value of the GridCell(row data) you can achieve this by writing a custom renderer to the GridCell and replacing the GridCellRenderer with your custom renderer. Please refer the below code snippet to achieve your requirement.

 
// Replacing your renderer in our cell renderer collection 
grid.CellRenderers.Remove("TextView"); 
grid.CellRenderers.Add("TextView", new CustomCell());

CustomRenderer.cs
public class CustomCell : GridCellTextViewRenderer 
{ 
    public CustomCell() 
    { 
 
    } 
 
    public override void OnInitializeDisplayView(DataColumnBase dataColumn, UILabel view) 
    { 
        base.OnInitializeDisplayView(dataColumn, view); 
        var rowData = dataColumn.RowData as DynamicModel; 
        // If you want to format all columns based on values row data 
        //if (Convert.ToInt32(Convert.ToString(rowData.Values["orderID"])) > 10) 
        //{ 
        //    view.TextColor = UIColor.Purple; 
        //    view.Font = UIFont.FromName("AmericanTypewriter-CondensedBold", 20f); 
        //} 
 
        // If you want to format only a particular column 
        if (Convert.ToInt32(Convert.ToString(rowData.Values["orderID"])) > 10 && dataColumn.GridColumn.MappingName == "Values[orderID]") 
        { 
            view.TextColor = UIColor.Purple; 
            view.Font = UIFont.FromName("AmericanTypewriter-CondensedBold", 20f); 
        } 
    } 
} 

We have prepared a sample based on your requirement where we have applied currency format to the first column, and have changed the text color, font size, font and font attributes if the value of the first column is greater than 10. Please find the sample from the below link.

Sample Link : http://www.syncfusion.com/downloads/support/forum/129747/ze/GettingStarted-81859564

In case your expectation of formatting is different from what we have explained above, please revert us with more details on what exactly you are trying to achieve so that we can help you better.

Regards,
Vimal Prabhu
 



CA Clint Anderson April 3, 2017 12:43 PM UTC

Our formatting is a little more complex than what the standard StringFormats allows.  We have a custom expression engine that takes in an expression and an object and generates the formatted string that needs to be displayed. 

In our current solution we do something like this when we are populating the cell with data:

if (viewCol.ColumnFormatExpression != null) { 
                //if an expression exists for the column, run the cell's value through it to get the output
                textCell.label.Text = viewCol.ColumnFormatExpression.execute (cellValue); 
      } else { 
           if (!StringUtils.IsEmpty (column.FormatType)) { 
                //this is similar to the StringFormats previously mentioned
                textCell.label.Text = IosRenderSupport.Format (cellValue, column.FormatMask, column.FormatType); 
           } else {
                //if we have neither an expression or specified format, just display the text/numeric value of the cell
                textCell.label.Text = StringUtils.getSafeStringNoNull (cellValue); 
           } 
      }




PS Pavithra  Sivakumar Syncfusion Team April 4, 2017 12:10 PM UTC

Hi Clint, 
 
Thanks for the update, 
 
We have understand your query of customizing the text by accessing the CellValue. You can access the CellValue in the renderer using the dataColumn parameter as dataColumn.CellValue. 
 
Refer the below code example for more details. 
 
public class CustomCell : GridCellTextViewRenderer 
{ 
     public CustomCell() 
     { 
 
     } 
     public override void OnInitializeDisplayView(DataColumnBase dataColumn, UILabel view) 
    { 
            base.OnInitializeDisplayView(dataColumn, view); 
            var rowData = dataColumn.RowData as DynamicModel; 
           
            if (Convert.ToInt32(Convert.ToString(rowData.Values["orderID"])) > 10 && dataColumn.GridColumn.MappingName == "Values[orderID]")// && Convert.ToInt32(Convert.ToString(rowData.Values["customerID"])) > 100) 
            { 
                view.TextColor = UIColor.Purple; 
                view.Font = UIFont.FromName("AmericanTypewriter-CondensedBold", 20f); 
                var value = dataColumn.CellValue; 
            } 
        } 
    } 
 
 
Regards, 
Pavithra S 
 


Loader.
Live Chat Icon For mobile
Up arrow icon