当前位置: 移动技术网 > IT编程>开发语言>c# > DataGridView带图标的单元格实现代码

DataGridView带图标的单元格实现代码

2019年07月18日  | 移动技术网IT编程  | 我要评论
目的: 扩展 c# winform 自带的表格控件,使其可以自动判断数据的上下界限值,并标识溢出。 这里使用的方法是:扩展 表格的列 对象:datagridview

目的:

扩展 c# winform 自带的表格控件,使其可以自动判断数据的上下界限值,并标识溢出。

这里使用的方法是:扩展 表格的列 对象:datagridviewcolumn。

1.创建类:decimalcheckcell

 /// <summary>
 /// 可进行范围检查的 数值单元格
 /// </summary>
 public class decimalcheckcell : datagridviewtextboxcell
 {
 private bool checkmaxvalue = false;
 private bool checkminvalue = false;
 private decimal maxvalue = 0;
 private decimal minvalue = 0;

 public decimal maxvalue
 {
  get { return maxvalue; }
  internal set { maxvalue = value; }
 }

 public decimal minvalue
 {
  get { return minvalue; }
  internal set { minvalue = value; }
 }

 public bool checkmaxvalue
 {
  get { return checkmaxvalue; }
  internal set { checkmaxvalue = value; }
 }
 
 public bool checkminvalue
 {
  get { return checkminvalue; }
  internal set
  {
  checkminvalue = value;
  }
 }


 public override object clone()
 {
  decimalcheckcell c = base.clone() as decimalcheckcell;
  c.checkmaxvalue = this.checkmaxvalue;
  c.checkminvalue = this.checkminvalue;
  c.maxvalue = this.maxvalue;
  c.minvalue = this.minvalue;
  return c;
 }

 protected override void paint(graphics graphics, rectangle clipbounds,
  rectangle cellbounds, int rowindex, datagridviewelementstates cellstate,
  object value, object formattedvalue, string errortext,
  datagridviewcellstyle cellstyle,
  datagridviewadvancedborderstyle advancedborderstyle,
  datagridviewpaintparts paintparts)
 {
  // paint the base content
  base.paint(graphics, clipbounds, cellbounds, rowindex, cellstate,
  value, formattedvalue, errortext, cellstyle,
  advancedborderstyle, paintparts);

  // 上下界限溢出判断
  if (this.rowindex < 0 || this.owningrow.isnewrow) // 行序号不为-1,且不是新记录行(貌似没用)
  return;
  if (value == null) return;

  decimal vcurvalue = convert.todecimal(value);
  bool overvalue = false;
  image img = null;
  if (checkmaxvalue)
  {
  overvalue = vcurvalue > maxvalue;
  img = vstest.properties.resources.undo; // 图片来自 添加的资源文件
  }
  if (checkminvalue && !overvalue)
  {
  overvalue = vcurvalue < minvalue;
  img = vstest.properties.resources.redo; // 图片来自 添加的资源文件
  }

  // 将图片绘制在 数值文本后面
  if (overvalue && img != null)
  {
  var vsize = graphics.measurestring(vcurvalue.tostring(), cellstyle.font);

  system.drawing.drawing2d.graphicscontainer container = graphics.begincontainer();
  graphics.setclip(cellbounds);
  graphics.drawimageunscaled(img, new point(cellbounds.location.x + (int)vsize.width, cellbounds.location.y));
  graphics.endcontainer(container);
  }
 }

 protected override bool setvalue(int rowindex, object value)
 {
  if (rowindex >= 0)
  {
  try
  {
   decimal vdeci = convert.todecimal(value); // 筛选非数字
   base.errortext = string.empty;
  }
  catch (exception ex)
  {
   base.errortext = "输入错误" + ex.message;
   return false;
  }
  }
  return base.setvalue(rowindex, value);
 }

 
 }

2.创建类:decimalcheckcolumn

 

 /// <summary>
 /// 可进行范围检查的 数值列
 /// </summary>
 public class decimalcheckcolumn : datagridviewcolumn
 {
  private bool checkmaxvalue = false;
  private bool checkminvalue = false;
  private decimal maxvalue = 0;
  private decimal minvalue = 0;

  public decimal maxvalue
  {
   get { return maxvalue; }
   set
   {
    maxvalue = value;
    (base.celltemplate as decimalcheckcell).maxvalue = value;
   }
  }

  public decimal minvalue
  {
   get { return minvalue; }
   set
   {
    minvalue = value;
    (base.celltemplate as decimalcheckcell).minvalue = value;
   }
  }

  /// <summary>
  /// 是否对值上界限进行检查,与maxvalue配合使用
  /// </summary>
  public bool checkmaxvalue
  {
   get { return checkmaxvalue; }
   set
   {
    checkmaxvalue = value;
    (base.celltemplate as decimalcheckcell).checkmaxvalue = value;
   }
  }
  /// <summary>
  /// 是否对值下界限进行检查,与minvalue配合使用
  /// </summary>
  public bool checkminvalue
  {
   get { return checkminvalue; }
   set
   {
    checkminvalue = value;
    (base.celltemplate as decimalcheckcell).checkminvalue = value;
   }
  }

  public decimalcheckcolumn()
   : base(new decimalcheckcell())
  {
   
  }

  public override object clone()
  {
   decimalcheckcolumn c = base.clone() as decimalcheckcolumn;
   c.checkmaxvalue = this.checkmaxvalue;
   c.checkminvalue = this.checkminvalue;
   c.maxvalue = this.maxvalue;
   c.minvalue = this.minvalue;

   return c;
  }

 }

3.现在就可以使用了,在窗体上拖一个 datagridview 控件,添加如下代码:

 private void testform_load(object sender, eventargs e)
  {
   initcontrolsproperties(); // 初始化

   // 绑定数据
   datatable dtabel = new datatable();
   dtabel.columns.add("id",typeof(int));
   dtabel.columns.add("testvalue",typeof(decimal));
   random rnd = new random();
   for (int i = 0; i < 10; i++) // 随机10个数
   {
    var vdr = dtabel.newrow();
    vdr[0] = i + 1;
    vdr[1] = rnd.next(50);
    dtabel.rows.add(vdr);
   }
   this.datagridview1.datasource = dtabel;
  }

  private void initcontrolsproperties()
  {
   decimalcheckcolumn columnroleid = new decimalcheckcolumn();
   columnroleid.datapropertyname = "id";
   columnroleid.defaultcellstyle.alignment = datagridviewcontentalignment.middleleft;
   columnroleid.name = "id";
   columnroleid.headertext = "序号";
   columnroleid.width = 50;
   this.datagridview1.columns.add(columnroleid);

   decimalcheckcolumn columnrolename = new decimalcheckcolumn();
   columnrolename.datapropertyname = "testvalue";
   columnrolename.defaultcellstyle.alignment = datagridviewcontentalignment.middleleft;
   columnrolename.name = "testvalue";
   columnrolename.headertext = "测试数据";
   columnrolename.width = 100;

   columnrolename.checkmaxvalue = true; // 进行最大值检查
   columnrolename.maxvalue = 41;
   columnrolename.checkminvalue = true; // 进行最小值检查
   columnrolename.minvalue = 7;

   this.datagridview1.columns.add(columnrolename);

   //this.datagridview1.allowusertoaddrows = false;
   //this.datagridview1.allowusertodeleterows = false;
   //this.datagridview1.readonly = true;
   this.datagridview1.autogeneratecolumns = false;

  }

运行效果如下图左所示

 那右边图是什么鬼?

现在还有一个问题没有解决:默认第一次加载出来的数据,并不能完全判断出是否超界限,有时会有一两个能判断,有时完全不能判断,但只需要用鼠标去点击各单元格,它又可以自动识别。暂时没有发现问题原因所在。

 以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网