Articles in this section
Category / Section

How to customize the Foreground color for cells in a column based on the cell content in Xamarin.Android DataGrid?

2 mins read

SfDataGrid displays all the text values in GridCells with a default Foreground color of RGB values 51. However SfDataGrid allows you to customize the Foreground color of the GridCells for the entire view and also to some specific cells based on conditions. 

To change the Foreground color of the GridCells for the entire view, you can use the SfDataGrid.GridStyle property and set the custom style to change the foreground color. For more details please refer our user documentation in the below link.

https://help.syncfusion.com/xamarin-android/sfdatagrid/styles

In case, if your requirement is to change the Foreground color based on the cell value, then you can achieve this by creating custom GridCell derived from GridCell in SfDataGrid. SfDataGrid also provides options to load views inside the GridCell or to draw the cell value directly in the canvas based on your application requirements. Drawing the contents directly in the canvas gives you much better performance.

 

Refer the below code example for loading views inside the GridCell to customize its Foreground color based on cell value. The foreground of the GridCells are applied with red color for negative values and black for positive values.

public class CustomGridCell : GridCell
{
  private TextView textview;
  public CustomGridCell(Context ctxt) : base(ctxt)
  { 
    textview = new TextView(ctxt);
    this.AddView(textview);
  }
 
  protected override void OnDraw(Canvas canvas)
  {
    base.OnDraw(canvas);
    if (this.DataColumn != null && DataColumn.CellValue != null && 
       textview.Text !=   DataColumn.CellValue.ToString())
   {
     textview.Text = (DataColumn.CellValue.ToString());
     if ((int)this.DataColumn.CellValue < 0)
          textview.SetTextColor(Color.Red);
     else
          textview.SetTextColor(Color.Black);
   }
}

 

 

Refer the below code example for directly drawing the cell values on the GridCell’s canvas. The foreground of the GridCells are applied with red color for values < 1010 and black for positive values while drawing in the canvas.

public class CustomGridCell:GridCell
{
    private Paint paintText;
    public CustomGridCell(Context ctxt) : base(ctxt)
    {
      paintText = new Paint(PaintFlags.AntiAlias | PaintFlags.LinearText);
      paintText.TextSize = 48; //Device dependent pixels 
    }
 
  protected override void OnDraw(Canvas canvas)
  {
    if (this.DataColumn != null && DataColumn.CellValue != null)
    {
      if ((int)this.DataColumn.CellValue < 1010)
      {
        paintText.Color=Color.Red;
      }
      else
      {
        paintText.Color=Color.Black;
      }
        canvas.DrawText(this.DataColumn.FormattedCellvalue.ToString(), 100, 100, paintText);
    }
  }
}

 

Refer the following screenshot for the final outcome

Table

Description automatically generated

Refer Sample:
https://www.syncfusion.com/downloads/support/directtrac/general/ze/cellforeground163312222


Conclusion

I hope you enjoyed learning about how to customize the Foreground color for cells in a column based on the cell content.


You can refer to our Xamarin.Android DataGrid feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications.

For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls.

If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forumsDirect-Trac, or feedback portal. We are always happy to assist you!

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please sign in to leave a comment
Access denied
Access denied