使用.Net Compact Framework 3.5在DataGrid中进行条件格式化

时间:2009-12-10 20:40:14

标签: c# .net .net-3.5 compact-framework

这在完整的.net版本中非常标准。我想绑定到一个对象集合,然后处理某种类型的RowDataBound事件,并根据其中一个对象属性更改行的背景颜色。这在Windows Mobile中是否可以使用.Net CF 3.5?

1 个答案:

答案 0 :(得分:0)

我遇到同样的麻烦,到目前为止这是我的解决方案:

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);
    }
}

艰难的方法:自己画画

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);
}

最后一种方法为您提供完全控制。请注意,格式字符串看起来像这样:

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

而不是

this.dataGridTextBoxColumn1.Format = "0000";

添加Coloums:

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

更改行高的唯一方法似乎是更改DataGrid.PreferedRowHeight,但这会设置所有行的高度。 根据您的需要,为每个列派生一个新类可能是个好主意。 这对我来说还在进行中,所以如果你有任何问题,请告诉我。 祝你好运; D