首页 > 代码库 > 1如何给devexpress的gridview控件绘制全选按钮

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 = http://www.mamicode.com/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 = http://www.mamicode.com/view.GetRowCellValue(i, view.Columns[filedName]);
if (_cellValue =http://www.mamicode.com/= 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 = http://www.mamicode.com/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 = http://www.mamicode.com/view.GetRowCellValue(i, view.Columns[filedName]);
if (_cellValue =http://www.mamicode.com/= null) continue;
if (string.IsNullOrEmpty(_cellValue.ToString().Trim())) continue;
bool _checkStatus = false;
if (bool.TryParse(_cellValue.ToString(), out _checkStatus))
{
if (_checkStatus)
count++;
}
}
return count;
}
}

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