很简单的方法,就是用js实现:
页面:
<asp:datagrid id="dgUserList" runat="server" Width="640px" BorderColor="White" PagerStyle-HorizontalAlign="Right"
AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False">
<AlternatingItemStyle BackColor="#F5F5F5"></AlternatingItemStyle>
<ItemStyle HorizontalAlign="Center"></ItemStyle>
<HeaderStyle HorizontalAlign="Center" ForeColor="White" BackColor="#4A95FD" Height="8"></HeaderStyle>
<PagerStyle HorizontalAlign="Right" Mode="NumericPages"></PagerStyle>
<Columns>
<asp:TemplateColumn>
<HeaderTemplate>
<asp:CheckBox id="chkAll" runat="server"></asp:CheckBox>
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox id="chkItem" runat="server"></asp:CheckBox>
</ItemTemplate>
</asp:TemplateColumn>
<asp:BoundColumn DataField="id" HeaderText="序号"></asp:BoundColumn>
<asp:BoundColumn DataField="username" HeaderText="用户名"></asp:BoundColumn>
<asp:BoundColumn DataField="workno" HeaderText="工号"></asp:BoundColumn>
<asp:BoundColumn DataField="dept" HeaderText="部门"></asp:BoundColumn>
</Columns>
<PagerStyle Visible="False" HorizontalAlign="Right" Mode="NumericPages"></PagerStyle>
</asp:datagrid>
后台代码:
private void dgUserList_PreRender(object sender, System.EventArgs e)
{
foreach (DataGridItem item in dgUserList.Controls[0].Controls)
{
if (item.ItemType == ListItemType.Header)
{
CheckBox chkAll=(CheckBox)item.FindControl("chkAll");
System.Text.StringBuilder strScript = new System.Text.StringBuilder("<script language='javascript'> \n");
strScript.Append(" function checkStatus() { \n");
strScript.Append(" var bAll = true; \n");
strScript.Append(" bAll = document.all('" + chkAll.ClientID + "').checked; \n");
for(int i=0; i<dgUserList.Items.Count ; i++)
{
strScript.Append(" document.all('" + dgUserList.Items[i].Cells[0].FindControl("chkItem").ClientID + "').checked = bAll; \n");
}
strScript.Append(" } \n");
strScript.Append("</script> \n");
if(!Page.IsClientScriptBlockRegistered("checkStatus"))
Page.RegisterClientScriptBlock("checkStatus",strScript.ToString());
chkAll.Attributes.Add("onclick","checkStatus()");
return;
}
}
<alternatingitemstyle backcolor="#F5F5F5"></alternatingitemstyle><itemstyle horizontalalign="Center"></itemstyle><headerstyle backcolor="#4A95FD" horizontalalign="Center" height="8" forecolor="White"></headerstyle><pagerstyle horizontalalign="Right" mode="NumericPages"></pagerstyle><columns><?xml:namespace prefix = asp /><templatecolumn><headertemplate><checkbox id="chkAll" runat="server"></checkbox></headertemplate><itemtemplate><checkbox id="chkItem" runat="server"></checkbox></itemtemplate></templatecolumn><boundcolumn headertext="序号" datafield="id"></boundcolumn><boundcolumn headertext="用户名" datafield="username"></boundcolumn><boundcolumn headertext="工号" datafield="workno"></boundcolumn><boundcolumn headertext="部门" datafield="dept"></boundcolumn></columns><pagerstyle horizontalalign="Right" mode="NumericPages" visible="False"></pagerstyle><alternatingitemstyle backcolor="#F5F5F5"></alternatingitemstyle><itemstyle horizontalalign="Center"></itemstyle><headerstyle backcolor="#4A95FD" horizontalalign="Center" height="8" forecolor="White"></headerstyle><pagerstyle horizontalalign="Right" mode="NumericPages"></pagerstyle><columns><templatecolumn><headertemplate><checkbox id="chkAll" runat="server"></checkbox></headertemplate><itemtemplate><checkbox id="chkItem" runat="server"></checkbox></itemtemplate></templatecolumn><boundcolumn headertext="序号" datafield="id"></boundcolumn><boundcolumn headertext="用户名" datafield="username"></boundcolumn><boundcolumn headertext="工号" datafield="workno"></boundcolumn><boundcolumn headertext="部门" datafield="dept"></boundcolumn></columns><pagerstyle horizontalalign="Right" mode="NumericPages" visible="False"></pagerstyle>
分享到:
相关推荐
项目中用到DataGrid, 需要在第一列添加checkbox, 可以多选、全选。 其中涉及的概念DataTemplate, DataGridCellStyle, DataGridCellControlTemplate,Binding, OnPropertyChanged等。
winform DataGridView表头带CheckBox全选;winform DataGridView表头带CheckBox全选
NULL 博文链接:https://taomujian.iteye.com/blog/294748
成功的实现了,datagrid中嵌套checkbox实现全选的功能。分享代码供大家分享。项目为flex3。
3,最后在datagrid中引用下面代码即可 import com.rj.dgrenderer.CheckBoxItemRenderer; import com.rj.dgrenderer.CheckBoxRenderer; 全选" editable="false" sortable="false" draggable="false" width="35" ...
Datagridview控件没有全选或取消全选的复选框,本资源用VB2015通过扩展DataGridViewColumnHeaderCell类,在列头绘制一个复选框checkbox控件,通过定义checkbox鼠标单击事件来实现行的全选或取消全选。checkbox还可以...
在DataGrid中实现了 简单的全选与非全选。
对DataGrid控件 增删改 全选 操作 详细说明用法 请看我的博客
flex Datagrid checkbox全选 Datagrid
NULL 博文链接:https://tony0101.iteye.com/blog/579404
在DataGrid中进行复选框的操作(全选和取消)
NULL 博文链接:https://yunzhongxia.iteye.com/blog/652902
最近懒 想在网上找个DataGrid ColumnHeader里面添加CheckBox全选,但是网上没找到,于是就自己写了一个这样的样式,具体功能实现你们就自己去写吧。 分享给大家。 有什么问题可以直接加WPF/Silverlight 找晴天就...
NULL 博文链接:https://yunzhongxia.iteye.com/blog/1145400
绝对好东西,datagrid 标题栏及 datagridcolumn 添加 checkbox
Flex dataGrid 全选、反选 checkBox 不是很完善,但很有借鉴意义
WPF中DataGrid里面的Checkbox实现单选关键代码, 文档里面的代码复制粘贴即可实现!本人也是项目需要写的一个功能11行代码实现此功能! 希望可以帮助到你们!
实现了向dataGrid中添加checkBox的功能,可以支持多选,单选,全选。
主要写了一个全选列,功能包括设置选择框的默认选择状态, 默认可用状态, 全选以及快速获取选中的值,呵呵!