DataGrid - Highlight Individual row?


How can I change the colour of an entire row, based on a value in the underlying data? Say, for example, I want to highlight some rows in red, to indicate an error.

I’m using the Xamarin bindings. I couldn’t find example code for this.



Greetings Program!

If you’re applying all RowStyles to your grid, use AlterStyleBeforeApplyingToCellAtCoordinate:

protected override void AlterStyleBeforeApplyingToCellAtCoordinate (ShinobiDataGrid grid, SDataGridCellStyle style, SDataGridCoord coordinate)
	// Get the cell.
	SDataGridCell cell = grid.VisibleCellAtCoordinate(coordinate);

	// Get the data for the row.
	StockPrice stock = model.Data[coordinate.Row.RowIndex];

	// Check if its been recently updated (or has an error) and that the
	// cell is not selected (otherwise you won't be able to tell if it's selected
	// when the new style is applied).
	if(stock.RecentlyUpdated && !cell.selected)
		style.BackgroundColor = UIColor.Yellow;

And if not, use PrepareCellForDisplay:

protected override void PrepareCellForDisplay (ShinobiDataGrid grid, SDataGridCell cell)
	SDataGridTextCell textCell = cell as SDataGridTextCell;
	StockPrice stock = model.Data[cell.Coordinate.Row.RowIndex];

	string title = cell.Coordinate.Column.Title.ToLower();
		case "symbol":
			textCell.TextField.Text = stock.Symbol;
		// case etc...

	if (stock.RecentlyUpdated)
		cell.backgroundColor = UIColor.Red



Thanks wizgod.

I’m using RowStyles, so I tried your first example. It worked great, until I scrolled the grid, and then the row stylings were lost.

Have I missed something?


Seems like using the RenderingDynamicData app wasn’t the best choice since stock.recentlyUpdated changes dynamically and regularly :slight_smile: but it does work in there and I tested it with the StylingTheGrid sample app this time :slight_smile: