【问题标题】:Conditional formatting in a DataGrid using .Net Compact Framework 3.5使用 .Net Compact Framework 3.5 在 DataGrid 中进行条件格式设置
【发布时间】:2010-12-25 10:15:09
【问题描述】:

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

【问题讨论】:

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


    【解决方案1】:

    我在自动取款机上遇到了同样的问题,到目前为止,这是我的解决方案:

    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";
    

    添加列:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-05-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-03
      • 1970-01-01
      相关资源
      最近更新 更多