DataGrid - Highlight Individual row?


#1

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.

Thanks


#2

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();
	switch(title)
	{
		case "symbol":
			textCell.TextField.Text = stock.Symbol;
			break;
		// case etc...
	}

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

Wg


#3

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?


#4

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:

Wg