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

DevExpress实现GridControl同步列头checkbox与列中checkbox状态

程序员文章站 2024-02-18 14:37:10
本文实例展示了devexpress实现gridcontrol同步列头checkbox与列中checkbox状态的方法,有一定的实用价值,具体方法如下: 主要功能代码如下:...

本文实例展示了devexpress实现gridcontrol同步列头checkbox与列中checkbox状态的方法,有一定的实用价值,具体方法如下:

主要功能代码如下:

/// <summary>
/// 同步列头checkbox与列中checkbox状态
/// </summary>
/// <param name="view">gridview</param>
/// <param name="fieldename">需要绘制checkbox的列名</param>
/// <param name="e">mouseeventargs</param>
public static void synccheckstatus(this gridview view, string fieldename, mouseeventargs e)
{
  /*说明:
   *在mousedown事件中使用
   *参考:https://www.devexpress.com/support/center/question/details/q354489
   *eg:
   *private void gvlampconfig_mousedown(object sender, mouseeventargs e)
   *{
   *gridview _view = sender as gridview;
   *_view.synccheckstatus(gccheckfieldname, e);
   *}
   */
  if (e.clicks == 1 && e.button == mousebuttons.left)
  {
 view.clearsorting();
 view.posteditor();
 gridhitinfo _info;
 point _pt = view.gridcontrol.pointtoclient(control.mouseposition);
 _info = view.calchitinfo(_pt);
 if (_info.incolumn && _info.column.fieldname.equals(fieldename))
 {
   if (getcheckedcount(view, fieldename) == view.datarowcount)
 unchekall(view, fieldename);
   else
 checkall(view, fieldename);
 }
  }
}
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 && !(_cellvalue is dbnull))
 if (_cellvalue == null) continue;
 if (string.isnullorempty(_cellvalue.tostring().trim())) continue;
 bool _checkstatus = false;
 if (bool.tryparse(_cellvalue.tostring(), out _checkstatus))
 {
   //if ((bool)_cellvalue)
   if (_checkstatus)
 count++;
 }
  }
  return count;
}
private static void checkall(gridview view, string fieldname)
{
  for (int i = 0; i < view.datarowcount; i++)
  {
 view.setrowcellvalue(i, view.columns[fieldname], true);
  }
}
private static void unchekall(gridview view, string fieldname)
{
  for (int i = 0; i < view.datarowcount; i++)
  {
 view.setrowcellvalue(i, view.columns[fieldname], false);
  }
}

代码使用方法如下:

private void gvlampconfig_mousedown(object sender, mouseeventargs e)
{
  gridview _view = sender as gridview;
  _view.synccheckstatus(gccheckfieldname, e);
}

代码运行效果如下:

DevExpress实现GridControl同步列头checkbox与列中checkbox状态