DevExpress 에서 GridControl 열 헤드 를 구현 하여 Checkbox 를 그 리 는 방법

3176 단어 DevExpressGridControl
본 고 는 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);
}

코드 실행 효 과 는 다음 그림 과 같 습 니 다.

좋은 웹페이지 즐겨찾기