关闭

定制 WinForm DataGrid单元格格式

885人阅读 评论(0) 收藏 举报
以前都是在.asp.net下开发程序,前段时间做一个winform项目,里面用到datagrid,要求datagrid中的每行的单元格式需要根据不同的要求显示不同的颜色,本来想通过设置datagrid的一些属性就可以搞定了,后来发现没有这么简单,要达到要求就需要重新实现DataGridColumnStyle 抽象类了,微软为DataGridColumnStyle 提供了一个实现子类为DataGridTextBoxColumn ,在DataGridTextBoxColumn 基础上来重写Paint(Graphics g, Rectangle bounds , CurrencyManager source , int rowNum, Brush backBrush ,    Brush foreBrush , bool alignToRight )方法来到达我们的要求.

using System;
using System.Windows.Forms;
using System.Collections;
using System.Drawing;
namespace LQF.RainVersionReceiver.WinUI
{
 /// <summary>
 /// GridColoredTextBoxColumn 的摘要说明。
 ///
 /// 定制DataGrid单元格样式
 /// </summary>
 [System.ComponentModel.DesignerCategory("Code")]
 public class GridColoredTextBoxColumn:DataGridTextBoxColumn
 {  
       
  /// <summary>
  /// 用于存放单元格背景色
  /// </summary>
  private System.Drawing.Color BackColor;

  
  /// <summary>
  /// 用于存放单元格前景色
  /// </summary>
  /// <returns></returns>
  private System.Drawing.Color ForeColor;

       
  /// <summary>
  /// 单元格值显示格式接口
  /// </summary>
  private IGridTextBoxColumnValue cellvalue=null;

  /// <summary>
  /// 单元格值显示格式
  /// </summary>
  /// <param name="icellvalue"> 单元格内容显示格式类</param>
  /// <param name="forecolor"> 单元格的前景色</param>
  /// <param name="backcolor"> 单元格的背景色</param>
  public GridColoredTextBoxColumn(IGridTextBoxColumnValue icellvalue,System.Drawing.Color forecolor,System.Drawing.Color backcolor)
  {
   //
   // TODO: 在此处添加构造函数逻辑
   //

   this.cellvalue=icellvalue;
   this.ForeColor=forecolor;
   this.BackColor =backcolor;
  }


  protected override void Paint(Graphics g, Rectangle bounds ,
   CurrencyManager source , int rowNum, Brush backBrush ,
   Brush foreBrush , bool alignToRight )
  
  {  
   string text=string.Empty;

   //模式重绘
   if (cellvalue!=null)
   {
    text = cellvalue.GetDisplayText(GetColumnValueAtRow(source, rowNum)); /没有提供格式化实例,/默认取单元格数据
   }
   else
   {
    text = GetColumnValueAtRow(source, rowNum).ToString(); //按格式化类实现的格式取单元格数据,                
   }

   backBrush = new SolidBrush(TextBox.BackColor); //背景色

   foreBrush = new SolidBrush(TextBox.ForeColor); //前景色

  
   backBrush = new SolidBrush((Color)BackColor);
   foreBrush = new SolidBrush((Color)ForeColor);
   
   base.PaintText(g, bounds, text, backBrush, foreBrush, alignToRight);
  }
  
 }
   
 /// <summary>
 /// 单元格内容显示格式接口,可根据单元格不同的显示格式要求实现其子类
 /// </summary>
 public interface IGridTextBoxColumnValue
 {
       /// <summary>
       /// 按子类实现的格式返回单元格内容
       /// </summary>
       /// <param name="val">单元格值</param>
       /// <returns>单元格值格式化后显示的文本</returns>
  string GetDisplayText(object val);
 }

    /// <summary>
    /// 单元格内容显示格式类
    /// </summary>
 public class GridTextBoxColumnValue:IGridTextBoxColumnValue
 {
  
  /// <summary>
  /// 按子类实现的格式返回单元格内容
  /// </summary>
  /// <param name="val">单元格值</param>
  /// <returns>单元格值格式化后显示的文本</returns>
  public string GetDisplayText(object val)
  {  
   string ret=val.ToString();
   
   //空值显示格式
   if  (val is DBNull)
   {
                 ret= "-" ;
   }

            //时间显示格式
   if  (val is System.DateTime)
   {

    ret= ((System.DateTime)val).ToString("HH:mm:ss");
   }
           
   //零值显示格式
   if  (val is System.String)
   {
 
    if ((string)val=="0" || (string)val=="0.0" || (string)val=="0.00") ret ="-";
   }

   return ret;
   
       
  }
 }

}

在winform中调用示例:

DataGridTableStyle ts1 = new DataGridTableStyle();
   ts1.MappingName ="交费表";

   ts1.HeaderBackColor=Color.Black;
   ts1.HeaderForeColor=Color.Red;
   ts1.HeaderFont= new System.Drawing.Font("宋体",
                                                     12F,
                                                     System.Drawing.FontStyle.Regular,
                                                     System.Drawing.GraphicsUnit.Point,
                                                     ((System.Byte)(0)));

   ts1.SelectionBackColor=Color.Black;
   ts1.SelectionForeColor=Color.Red;

   ts1.AlternatingBackColor = Color.Black ;
          
   ts1.GridLineColor=Color.Red;
   

            ts1.BackColor=Color.Black;
   ts1.ForeColor=Color.Yellow ;
   ts1.AllowSorting=false;
            
    GridTextBoxColumnValue text=new GridTextBoxColumnValue();  //实例化一个格式化子类

   DataGridColumnStyle TextCol = new GridColoredTextBoxColumn(text,System.Drawing.Color.Yellow,System.Drawing.Color.Black);
   TextCol.MappingName = "code"

   TextCol.HeaderText = " 代 码";
   TextCol.Width = 58;
   TextCol.ReadOnly=true;
   ts1.GridColumnStyles.Add(TextCol);
     
   // Add a second column style.
   TextCol = new GridColoredTextBoxColumn(text,System.Drawing.Color.Yellow ,System.Drawing.Color.Black);
   TextCol.MappingName = "name"   

   TextCol.HeaderText = " 名 称 ";
   TextCol.Width = 100;
   TextCol.ReadOnly=true;
   ts1.GridColumnStyles.Add(TextCol);

   // Add a second column style.
   TextCol = new GridColoredTextBoxColumn(text,System.Drawing.Color.Red  ,System.Drawing.Color.Black);
   TextCol.MappingName = "new_price";
   TextCol.HeaderText = "价 格 ";
   TextCol.Width = 68;
   TextCol.ReadOnly=true;
   ts1.GridColumnStyles.Add(TextCol);

   // Add a second column style.
   TextCol =new GridColoredTextBoxColumn(text,System.Drawing.Color.Tomato ,System.Drawing.Color.Black);
   TextCol.MappingName ="makup" ;

    TextCol.HeaderText = " 涨 幅% ";
   TextCol.Width =68;
   TextCol.ReadOnly=true;
   ts1.GridColumnStyles.Add(TextCol);

   // Add a second column style.
   TextCol =new GridColoredTextBoxColumn(text,System.Drawing.Color.GreenYellow ,System.Drawing.Color.Black);
   TextCol.MappingName ="amount";
   TextCol.HeaderText = "     金 额    ";
   TextCol.Width = 180;
   TextCol.ReadOnly=true;
   ts1.GridColumnStyles.Add(TextCol);

   // Add a second column style.
   TextCol = new GridColoredTextBoxColumn(text,System.Drawing.Color.Turquoise ,System.Drawing.Color.Black);
   TextCol.MappingName = "recordtime";
   TextCol.HeaderText = " 时间 ";
   TextCol.Width =100;
   TextCol.ReadOnly=true;
   ts1.GridColumnStyles.Add(TextCol);

   
   /* Use a PropertyDescriptor to create a formatted
   column. First get the PropertyDescriptorCollection
   for the data source and data member. */
   //   PropertyDescriptorCollection pcol = this.BindingContext
   //    [myDataSet, "Customers.custToOrders"].GetItemProperties();
   //
   //   /* Create a formatted column using a PropertyDescriptor.
   //   The formatting character "c" specifies a currency format. */    
   //   DataGridColumnStyle csOrderAmount =
   //    new DataGridTextBoxColumn(pcol["OrderAmount"], "c", true);
   //   csOrderAmount.MappingName = "OrderAmount";
   //   csOrderAmount.HeaderText = "Total";
   //   csOrderAmount.Width = 100;
   //   ts2.GridColumnStyles.Add(csOrderAmount);

   /* Add the DataGridTableStyle instances to
   the GridTableStylesCollection. */
   this.gridList.TableStyles.Add(ts1);
   this.gridList.Font =new System.Drawing.Font("宋体",
    12F,
    System.Drawing.FontStyle.Regular,
    System.Drawing.GraphicsUnit.Point,
    ((System.Byte)(0)));
   
   this.gridList.BackgroundColor=Color.Black;
   this.gridList.CaptionBackColor=Color.Black;
   

   this.gridList.ParentRowsBackColor=Color.Black;
   this.gridList.ParentRowsForeColor=Color.Red;

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:9453次
    • 积分:101
    • 等级:
    • 排名:千里之外
    • 原创:0篇
    • 转载:10篇
    • 译文:0篇
    • 评论:2条
    文章存档