C# 中的DataGridView控件如何设置成具有复选框功能?

 我来答
百度网友9463fa8
2018-01-05 · TA获得超过164个赞
知道小有建树答主
回答量:257
采纳率:100%
帮助的人:121万
展开全部
<Columns>
<asp:TemplateColumn HeaderText="Checked" HeaderStyle-Width="30">
<HeaderTemplate>
<input id="chkAllInBox" type="checkbox" onclick="selectAll('chkAllInBox','<%# this.grdList.ClientID %>')" />
</HeaderTemplate>
<ItemTemplate>
<input type="checkbox" id="chids" value='<%# DataBinder.Eval(Container, "DataItem.ID") %>'
runat="server" name="chids" />
</ItemTemplate>
<HeaderStyle Width="30px"></HeaderStyle>
<ItemStyle Font-Bold="False" Font-Italic="False" Font-Overline="False" Font-Strikeout="False"
Font-Underline="False" HorizontalAlign="Center" />
</asp:TemplateColumn>
<asp:BoundColumn DataField="ID" SortExpression="ID" HeaderText="id" Visible="false">
</asp:BoundColumn>
<asp:BoundColumn DataField="EventCount" SortExpression="EventCount" HeaderText='数量'></asp:BoundColumn>
<asp:BoundColumn DataField="DomainName" HeaderText="类型" SortExpression="DomainName">
</asp:BoundColumn>
<asp:BoundColumn DataField="CalItemName" SortExpression="CalItemName" HeaderText="计算项">
</asp:BoundColumn>
<asp:BoundColumn DataField="CalUnitName" SortExpression="CalUnitName" HeaderText="地区或单位">
</asp:BoundColumn>
<asp:BoundColumn DataField="EventDesc" SortExpression="EventDesc" HeaderText="事件描述">
</asp:BoundColumn>
<asp:BoundColumn DataField="Month" SortExpression="Month" HeaderText="上报月份"></asp:BoundColumn>
<asp:BoundColumn DataField="DraftWriter" SortExpression="DraftWriter" HeaderText="填写人">
</asp:BoundColumn>
<asp:BoundColumn DataField="DraftWriteTime" SortExpression="DraftWriteTime" HeaderText="填写时间"
DataFormatString='{0:d}'></asp:BoundColumn>
<%--<asp:TemplateColumn>
<HeaderTemplate>
<asp:Label ID="lblPar1Name" runat="server"></asp:Label>
</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="lblPar1" runat="server"></asp:Label>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn>
<HeaderTemplate>
<asp:Label ID="lblPar2Name" runat="server"></asp:Label>
</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="lblPar2" runat="server"></asp:Label>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn>
<HeaderTemplate>
<asp:Label ID="lblPar3Name" runat="server"></asp:Label>
</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="lblPar3" runat="server"></asp:Label>
</ItemTemplate>
</asp:TemplateColumn>--%>
<asp:BoundColumn DataField="Par1" SortExpression="Par1" HeaderText="批次" Visible="False">
</asp:BoundColumn>
<asp:BoundColumn DataField="Par2" SortExpression="Par2" HeaderText="Par2" Visible="False">
</asp:BoundColumn>
<asp:BoundColumn DataField="Par3" SortExpression="Par3" HeaderText="Par3" Visible="False">
</asp:BoundColumn>
<asp:TemplateColumn HeaderStyle-Width="40" ItemStyle-HorizontalAlign="Center" HeaderText="编辑">
<ItemTemplate>
<asp:HyperLink ID="hlkEditInfo" runat="server" ImageUrl="~/images/edit.gif" NavigateUrl='<%# EditUrl("ItemId",DataBinder.Eval(Container, "DataItem.ID").ToString(),"Edit") %>'></asp:HyperLink>
</ItemTemplate>
<HeaderStyle Width="40px"></HeaderStyle>
<ItemStyle HorizontalAlign="Center"></ItemStyle>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderStyle-Width="40" ItemStyle-HorizontalAlign="Center" HeaderText="删除">
<ItemTemplate>
<asp:LinkButton runat="server" ID="lbnDelInfo" CommandName="CommandDelete" BorderStyle="None">
<asp:Image runat="server" ID="imgDelete" BorderStyle="None" ImageUrl="~\images\button_delete.gif"
ImageAlign="AbsMiddle"></asp:Image>
</asp:LinkButton>
</ItemTemplate>
<HeaderStyle Width="40px"></HeaderStyle>
<ItemStyle HorizontalAlign="Center"></ItemStyle>
</asp:TemplateColumn>
</Columns>

<script language="javascript">
//全选
function selectAll(SourceID, ControlID) {
var sourceBox = document.getElementById(SourceID);
var theBox = document.all(ControlID);

elm = theBox.getElementsByTagName('Input');
for (i = 0; i < elm.length; i++) {
if (elm[i].type == "checkbox") {
elm[i].checked = sourceBox.checked;
}
}
}
//选择删除检测
function CheckNULL(ControlID) {
var isNULL = false;
var theBox = document.all(ControlID);
elm = theBox.getElementsByTagName('Input');

for (var i = 0; i < elm.length; i++) {
var e = elm[i];
if (e.type == "checkbox" && e.checked == true && e.id != "chkAllInBox") {
isNULL = true;
}
}
if (isNULL == false) {
alert('请选择删除项!');
return false;
}
else {
return confirm('确定要删除信息?');
}
}
</script>
本回答被网友采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式