欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

DevExpress实现GridControl列头绘制Checkbox的方法

程序员文章站 2024-02-18 12:07:58
本文实例展示了devexpress实现gridcontrol列头绘制checkbox的方法,具体实现方法如下: 主要功能代码如下: ///

本文实例展示了devexpress实现gridcontrol列头绘制checkbox的方法,具体实现方法如下:

主要功能代码如下:

/// <summary>
/// 为列头绘制checkbox
/// </summary>
/// <param name="view">gridview</param>
/// <param name="checkitem">repositoryitemcheckedit</param>
/// <param name="fieldname">需要绘制checkbox的列名</param>
/// <param name="e">columnheadercustomdraweventargs</param>
public static void drawheadercheckbox(this gridview view, repositoryitemcheckedit checkitem, string fieldname, columnheadercustomdraweventargs e)
{
  /*说明:
   *参考:https://www.devexpress.com/support/center/question/details/q354489
   *在customdrawcolumnheader中使用
   *eg:
   * private void gvcabchdetail_customdrawcolumnheader(object sender, devexpress.xtragrid.views.grid.columnheadercustomdraweventargs e)
   * {
   * gridview _view = sender as gridview;
   * _view.drawheadercheckbox(checkitem, "check", e);
   * }
   */
  if (e.column != null && e.column.fieldname.equals(fieldname))
  {
 e.info.innerelements.clear();
 e.painter.drawobject(e.info);
 drawcheckbox(checkitem, e.graphics, e.bounds, getcheckedcount(view, fieldname) == view.datarowcount);
 e.handled = true;
  }
}
private static void drawcheckbox(repositoryitemcheckedit checkitem, graphics g, rectangle r, bool checked)
{
  checkeditviewinfo _info;
  checkeditpainter _painter;
  controlgraphicsinfoargs _args;
  _info = checkitem.createviewinfo() as checkeditviewinfo;
  _painter = checkitem.createpainter() as checkeditpainter;
  _info.editvalue = checked;

  _info.bounds = r;
  _info.paintappearance.forecolor = color.black;
  _info.calcviewinfo(g);
  _args = new controlgraphicsinfoargs(_info, new devexpress.utils.drawing.graphicscache(g), r);
  _painter.draw(_args);
  _args.cache.dispose();
}
private static int getcheckedcount(gridview view, string filedname)
{
  int count = 0;
  for (int i = 0; i < view.datarowcount; i++)
  {
 object _cellvalue = view.getrowcellvalue(i, view.columns[filedname]);
 if (_cellvalue == null) continue;
 if (string.isnullorempty(_cellvalue.tostring().trim())) continue;
 bool _checkstatus = false;
 if (bool.tryparse(_cellvalue.tostring(), out _checkstatus))
 {
   if (_checkstatus)
 count++;
 }
  }
  return count;
}

代码使用方法如下:

repositoryitemcheckedit checkitem = new repositoryitemcheckedit();
const string gccheckfieldname = "checked";
private void gvlampconfig_customdrawcolumnheader(object sender, columnheadercustomdraweventargs e)
{
  gridview _view = sender as gridview;
  _view.drawheadercheckbox(checkitem, gccheckfieldname, e);
}

代码运行效果如下图所示:

DevExpress实现GridControl列头绘制Checkbox的方法