Ive been trying to change the background color of a row in the Compact Framework DataGrid and have found little success since the DataGrid on .NET CF is limited compared to
Alright I went back and found my code from years ago where I did this on the desktop, before the more advanced DataGridView came out, etc.
First of all there is this tutorial from Microsoft Customizing the Windows Forms DataGrid, which explains how to highlight an entire row.
I looked at my code and I had to add a custom column style for each column, fire an event to the main form which I handled, and then determined the proper color for that record. Then, I set the args.Color property and the DataGridColumn would draw the correct color. So yes, you have to actually have each column be your custom formatable class, then your application logic can handle the event, get the record data and determine the color
** Update: here's a simple example **
public partial class Form1 : Form
{
FormattableTextBoxColumn firstNameColumn = new FormattableTextBoxColumn();
FormattableTextBoxColumn lastNameColumn = new FormattableTextBoxColumn();
public Form1()
{
InitializeComponent();
// add first name col
firstNameColumn.MappingName = "FirstName";
dataGridTableStyle1.GridColumnStyles.Add(firstNameColumn);
firstNameColumn.SetCellFormat += new FormatCellEventHandler(ColumnSetCellFormat);
// add last name col
lastNameColumn.MappingName = "LastName";
lastNameColumn.SetCellFormat += new FormatCellEventHandler(ColumnSetCellFormat);
dataGridTableStyle1.GridColumnStyles.Add(lastNameColumn);
// This just sets up a dummy data source, since I don't have a database in this example
List<PersonTest> peopleList = new List<PersonTest>();
peopleList.Add(new PersonTest
{
FirstName = "Alan",
LastName = "QQQQQ",
HighlightPerson = true
});
peopleList.Add(new PersonTest
{
FirstName = "John",
LastName = "Smith",
HighlightPerson = false
});
BindingSource peopleDataSource = new BindingSource();
peopleDataSource.DataSource = peopleList;
dataGridTableStyle1.MappingName = peopleDataSource.GetListName(null);
dataGrid1.DataSource = peopleDataSource;
}
// I'll cache this brush in the form, just make sure to dispose it (see designer.cs disposing)
SolidBrush highlightBrush = new SolidBrush(Color.Yellow);
// here is the event you can handle to determine the color of your row!
private void ColumnSetCellFormat(object sender, DataGridFormatCellEventArgs e)
{
if ((e.Source.List[e.Row] as PersonTest).HighlightPerson)
e.BackBrush = highlightBrush;
}
// example test class
public class PersonTest
{
public String FirstName { get; set; }
public String LastName { get; set; }
public bool HighlightPerson { get; set; }
}
}
And the custom data grid column
public class FormattableTextBoxColumn : DataGridTextBoxColumn
{
public event FormatCellEventHandler SetCellFormat;
protected override void Paint(Graphics g, Rectangle bounds, CurrencyManager source, int rowNum, Brush backBrush, Brush foreBrush, bool alignToRight)
{
DataGridFormatCellEventArgs e = new DataGridFormatCellEventArgs(rowNum, source);
e.ForeBrush = foreBrush;
e.BackBrush = backBrush;
OnSetCellFormat(e);
base.Paint(g, bounds, source, rowNum, e.BackBrush, e.ForeBrush, alignToRight);
}
private void OnSetCellFormat(DataGridFormatCellEventArgs e)
{
FormatCellEventHandler handler = SetCellFormat;
if (handler != null)
handler(this, e);
}
}
You'll also need this DataGridCellEventArgs.cs
public delegate void FormatCellEventHandler(object sender, DataGridFormatCellEventArgs e);
public class DataGridFormatCellEventArgs : EventArgs
{
public int Row;
public CurrencyManager Source;
public Brush BackBrush;
public Brush ForeBrush;
public DataGridFormatCellEventArgs(int row, CurrencyManager manager)
{
this.Row = row;
this.Source = manager;
}
}
Here's an example project for you:
DataGridTest.zip
I haven't worked with CF, but I thought I would throw this out there... If you can access the cell, wouldn't the row be it's NamingContainer
? If so you could drill up to the row and apply a style or add an attribute with a CSS class.