c#中GridView中checkbox如何全选

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c#中GridView中checkbox如何全选相关的知识,希望对你有一定的参考价值。

为什么CHECKBOX不被选中,没有被钩上 ,怎样检测被钩上了

用JS实现,具体代码为:
function
doSelect()

var
dom=document.all;
var
el=event.srcElement;
if(el.id.indexOf("全选框的ID")>=0&&el.tagName=="INPUT"&&el.type.toLowerCase()=="checkbox")

var
ischecked=false;
if(el.checked)
ischecked=true;
for(i=0;i

if(dom[i].id.indexOf("模板列里CHECKBOX的ID")>=0&&dom[i].tagName=="INPUT"&&dom[i].type.toLowerCase()=="checkbox")
dom[i].checked=ischecked;



再在模板列里CHECKBOX的ONCLICK=DOSELECT()
参考技术A 用JS实现,具体代码为:
function doSelect()

var dom=document.all;
var el=event.srcElement;
if(el.id.indexOf("全选框的ID")>=0&&el.tagName=="INPUT"&&el.type.toLowerCase()=="checkbox")

var ischecked=false;
if(el.checked)
ischecked=true;
for(i=0;i<dom.length;i++)

if(dom[i].id.indexOf("模板列里CHECKBOX的ID")>=0&&dom[i].tagName=="INPUT"&&dom[i].type.toLowerCase()=="checkbox")
dom[i].checked=ischecked;




再在模板列里CHECKBOX的ONCLICK=DOSELECT()
参考技术B 循环读GridView的所有行,用findcontrol方法找到每一行的checkbox,设置checkbox选中就可以了 参考技术C 比如0列是选择的,那么触发事件里加上。
for(int i = 0; i < gridview.rows.count; i++)

((..checkbos)gridview[i,0]).checked = true;


我这个是直接写的,大概就是这样了,

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;
}
}

以上是关于c#中GridView中checkbox如何全选的主要内容,如果未能解决你的问题,请参考以下文章

如何使用jquery实现gridview中checkbox(复选框)的全选功能?

gridview中checkbox的全选与全不选

.net中点击全选按钮实现GridView中复选框全选

js 脚本怎样实现checkbox的全选,反选,类似邮箱中邮件的全选后删除移动

winform中的GridView列头如何添加check控件,进行控制全选

GridView 中的全选不起作用