繁体   English   中英

使用.Net Compact Framework 3.5的DataGrid中的条件格式

[英]Conditional formatting in a DataGrid using .Net Compact Framework 3.5

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

我在atm遇到了同样的麻烦,这是到目前为止的解决方法:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM