将DataGrid中满足条件的行设为不同的背景色(WinForm).

public void  DataGridCellColor()
{
//.....
    private void BindToGrid()
    {
//获取数据源
    colText = new DataGridColoredTextBoxColumn(1);
    colText.HeaderText = "出库单编号";
    colText.Width = 120;
    colText.MappingName = "out_storageNumber";
    colText.TextBox.DoubleClick += new EventHandler(DataGrid_DoubleClick);
    tblStyle.GridColumnStyles.Add(colText);
    colText.CheckCellColor += new CellColorEventHandler(SetCellColor);

    colText = new DataGridColoredTextBoxColumn(2);
    colText.HeaderText = "日期";
    colText.MappingName = "theDate";
    colText.TextBox.DoubleClick += new EventHandler(DataGrid_DoubleClick);
    tblStyle.GridColumnStyles.Add(colText);
    colText.CheckCellColor += new CellColorEventHandler(SetCellColor);

//...
    gatheringGrid.TableStyles.Add(tblStyle);
    }

// 判断当前行是否满足条件, 并设置相应的背景色.
  public void SetCellColor(object sender, DataGridCellColorEventArgs e)
  {
   try
   {
    DateTime dt = (DateTime)gatheringGrid[e.Row,2];
    int intAccountSession = 27;
    if(!(gatheringGrid[e.Row,3] is DBNull))
     intAccountSession = (int)gatheringGrid[e.Row,3];

    TimeSpan ts = DateTime.Parse(Global.g_SystemDateTime) - dt;
    if(ts.Days >= intAccountSession)
    {
     e.BackColor = Color.Red;
    }
    else if(ts.Days >= intAccountSession - AccountWarning)
    {
     e.BackColor = Color.YellowGreen;
    }
   }
   catch{}
  }
}
//定义事件数据
 public class DataGridCellColorEventArgs : EventArgs
 {
  private int _column;
  private int _row;
  private Color _backcolor;

  public DataGridCellColorEventArgs(int row, int col, Color val)
  {
   _row = row;
   _column = col;
   _backcolor = val;
  }

  public int Column
  {
   get{ return _column;}
   set{ _column = value;}
  }
  public int Row
  {
   get{ return _row;}
   set{ _row = value;}
  }
  public Color BackColor
  {
   get{ return _backcolor;}
   set{ _backcolor = value;}
  }
 }

// 设置委托. 
public delegate void CellColorEventHandler(object sender, DataGridCellColorEventArgs e);

//继承样式列.
 public class DataGridColoredTextBoxColumn : DataGridTextBoxColumn
 {
  private int _col;
  public event CellColorEventHandler CheckCellColor;

  public DataGridColoredTextBoxColumn(int column)
  {
   _col = column;
  }

  protected override void Paint(System.Drawing.Graphics g, System.Drawing.Rectangle bounds, System.Windows.Forms.CurrencyManager source, int rowNum, System.Drawing.Brush backBrush, System.Drawing.Brush foreBrush, bool alignToRight)
  {

   if(CheckCellColor != null)
   {
    //设置当前行的背景色.
    DataGridCellColorEventArgs e = new DataGridCellColorEventArgs(rowNum, _col, Color.White);
    CheckCellColor(this, e);

     backBrush =  new SolidBrush(e.BackColor);
   }

   base.Paint(g, bounds, source, rowNum, backBrush, foreBrush, alignToRight);
  }

  protected override void Edit(System.Windows.Forms.CurrencyManager source, int rowNum, System.Drawing.Rectangle bounds, bool readOnly, string instantText, bool cellIsVisible)
  {
   //do nothing
   base.Edit(source,rowNum,bounds,readOnly,instantText,cellIsVisible);
  }
 }

转载于:https://www.cnblogs.com/josson/archive/2004/06/12/15198.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值