views:

391

answers:

1

This is very standard in the full .net version. I want to Bind to a collection of objects and then handle a RowDataBound event of some sort and change the background color of the row based upon one of the objects properties. Is this possible in Windows Mobile using the .Net CF 3.5?

A: 

I'm having the same trouble atm, here's my soloution so far:

public class DataGridExtendedTextBoxColumn : DataGridTextBoxColumn
{
    // I use the Form to store Brushes and the Font, feel free to handle it differently.
    Form1 parent;

    public DataGridExtendedTextBoxColumn(Form1 parent)
    {
        this.parent = parent;
    }

    // You'll need to override the paint method
    // The easy way: only change fore-/backBrush
    protected override void Paint(Graphics g, Rectangle bounds, CurrencyManager source, int rowNum, Brush backBrush, Brush foreBrush, bool alignToRight)
    {
        base.Paint(g, bounds, source, rowNum, parent.redBrush, parent.fontBrush, alignToRight);
    }
}

The hard way: draw it yourself

protected override void Paint(Graphics g, Rectangle bounds, CurrencyManager source, int rowNum, Brush backBrush, Brush foreBrush, bool alignToRight)
{
    // Background
    g.FillRectangle(parent.redBrush, bounds);

    // Get and format the String
    StringFormat sf = new StringFormat();
    DataRowView dataRowView = (DataRowView)source.List[rowNum];
    DataRow row = dataRowView.Row;
    Object value = row[this.MappingName];
    String str;
    if (value.Equals(System.DBNull.Value))
    {
        str = this.NullText;
    }
    else if (this.Format.Length != 0)
    {
        // NOTE: Formatting is handled differently!
        str = String.Format(this.Format, value);
    }
    else
    {
        str = value.ToString();
    }

    // Draw the String
    g.DrawString(str, parent.font, parent.fontBrush, new RectangleF(bounds.X, bounds.Y, bounds.Width, bounds.Height));

    //base.Paint(g, bounds, source, rowNum, parent.redBrush, parent.fontBrush, alignToRight);
}

The last approach gives you complete controll. Note that the format string would look something like this:

this.dataGridTextBoxColumn1.Format = "{0:0000}";

Instead of

this.dataGridTextBoxColumn1.Format = "0000";

To add the Coloums:

// The "this" is due to the new constructor
this.dataGridTextBoxColumn1 = new DataGridExtendedTextBoxColumn(this);
this.dataGridTableStyle1.GridColumnStyles.Add(this.dataGridTextBoxColumn1);

The only way to change the height of the rows seems to change DataGrid.PreferedRowHeight but this set the height for all rows. Depending on your needs it might be a good idea to derive a new class for every column. This is still work in progress for me, so if you've got any tipps let me know, please. Good luck with that ;D

Mene