• 1如何给devexpress的gridview控件绘制全选按钮


    1 首先注册gridview的this.edibandedGridView.CustomDrawColumnHeader += EdibandedGridView_CustomDrawColumnHeader事件,然后在事件中写入如下代码:

    private void EdibandedGridView_CustomDrawColumnHeader(object sender, ColumnHeaderCustomDrawEventArgs e)
    {
    RepositoryItemCheckEdit checkItem = new RepositoryItemCheckEdit();
    GridView _view = sender as GridView;
    _view.DrawHeaderCheckBox(checkItem, "Check", e);
    }

    其中DrawHeaderCheckBox方法为其扩展方法:

    public static void DrawHeaderCheckBox(this GridView view, RepositoryItemCheckEdit checkItem, string fieldName, ColumnHeaderCustomDrawEventArgs 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, getCheckedCount2(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();
    }

    然后注册 this.edibandedGridView.MouseDown += EdibandedGridView_MouseDown事件实现全选和单选:

    private void EdibandedGridView_MouseDown(object sender, MouseEventArgs e)
    {
    GridView _view = sender as GridView;
    _view.SyncCheckStatus("Check", e);
    }

    SyncCheckStatus方法为扩张方法。DrawHeaderCheckBoxExtension类为所有所需方法的封装类

    public static class DrawHeaderCheckBoxExtension
    {
    public static void SyncCheckStatus(this GridView view, string fieldeName, MouseEventArgs 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) 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++)
    {

    var row = view.GetRow(i) as GIDManager.Utility.GIDAPIDataServiceAPI.EDISearchEntity;
    if (row!=null)
    {
    row.Check = true;
    }

    }

    view.RefreshData();
    }
    private static void UnChekAll(GridView view, string fieldName)
    {
    for (int i = 0; i < view.DataRowCount; i++)
    {
    var row = view.GetRow(i) as GIDManager.Utility.GIDAPIDataServiceAPI.EDISearchEntity;
    if (row != null)
    {
    row.Check = false;
    }
    //view.SetRowCellValue(i, col, false);
    }

    view.RefreshData();

    }
    public static void DrawHeaderCheckBox(this GridView view, RepositoryItemCheckEdit checkItem, string fieldName, ColumnHeaderCustomDrawEventArgs 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, getCheckedCount2(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 getCheckedCount2(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;
    }
    }

  • 相关阅读:
    nginx 超时配置、根据域名、端口、链接 配置不同跳转
    nginx 作用,初认识
    JVM理解
    使用开发IDE生成一个springboot工程。
    到spring官网创建第一个springboot工程
    linux 忘记root密码怎么处理。
    学习重新开始
    共同父域下的单点登录
    Bootstrap 与 Jquery validate 结合使用——多个规则实现
    Bootstrap 与 Jquery validate 结合使用——简单实现
  • 原文地址:https://www.cnblogs.com/mibing/p/7344305.html
Copyright © 2020-2023  润新知