.NET开发

本类阅读TOP10

·NHibernate快速指南(翻译)
·vs.net 2005中文版下载地址收藏
·【小技巧】一个判断session是否过期的小技巧
·VB/ASP 调用 SQL Server 的存储过程
·?dos下编译.net程序找不到csc.exe文件
·通过Web Services上传和下载文件
·学习笔记(补)《.NET框架程序设计(修订版)》--目录
·VB.NET实现DirectDraw9 (2) 动画
·VB.NET实现DirectDraw9 (1) 托管的DDraw
·建站框架规范书之——文件命名

分类导航
VC语言Delphi
VB语言ASP
PerlJava
Script数据库
其他语言游戏开发
文件格式网站制作
软件工程.NET开发
DataGrid中由某列的值设定行的颜色

作者:未知 来源:月光软件站 加入时间:2005-2-28 月光软件站

今天真是的,又被界面搞的晕头转向.

为了实现.Net window DataGrid 的某一行可以根据该行某列的值的内容设定该行颜色的功能.

先贴一个连接,里面有DataGrid很多功能扩充的解决方案Windows Forms Datagrid

不过没有我这个需求的解决方案,最后终于还是在同事的帮助下搞定了.


由某一个单元格的值设定该单元格的颜色的实现我就不贴了,上面的连接里面有解决方案.
下面是由某列的值设定整行颜色的一个解决方案. 关键是在定制的DataGridTextBoxColumn里面添加一个DataView的属性,另外重载Paint() . 
在使用DataGridTextBoxColumn的时候,将DataGrid绑定的DataView赋值给它.

public class public class DataGridColoredTextBoxColumn : DataGridTextBoxColumn
 {
  private  System.Data.DataView m_bindDataView;
  public DataView BindingDataView
  {
   get
   {
    return m_bindDataView;
   }
   set
   {
    m_bindDataView = value;
   }
  }

  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)
  {
   // the idea is to conditionally set the foreBrush and/or backbrush
   // depending upon some crireria on the cell value
   // Here, we color anything that begins with a letter higher than 'F'
   try
   {
     //从DataView中取值,"ItemType"为行的名称
     string colValue = this.BindingDataView[rowNum]["ItemType"].ToString();     
     char val = colValue[0];

     if( val > 'F' ) //如果首字母大于 'F'
     {
      backBrush = new SolidBrush(Color.BlueViolet );
      foreBrush = new SolidBrush(Color.White);
     }
   }
   catch(Exception ex)
   {
    //empty catch
   }
   finally
   {
    // make sure the base class gets called to do the drawing with
    // the possibly changed brushes
    base.Paint(g, bounds, source, rowNum, backBrush, foreBrush, alignToRight);
   }
  }
 
 }


使用的例子
 DataGridColoredTextBoxColumn colExceptionType = new DataGridColoredTextBoxColumn();
   colItemType.BindingDataView = dtOrderItem.DefaultView; //将table的view赋值
   colItemType.HeaderText =“ItemType”;
   colItemType.MappingName = “ItemType“;
   colItemType.Width = 90;
   colItemType.NullText = "";
   tablestyle.GridColumnStyles.Add(colItemType);




相关文章

相关软件