本篇介绍:ASPxGridView 结合CheckBox实现多选 ,实质跟gridview是一样的:遍历ASPxGridView 找到每行的CheckBox,让后做处理。不过ASPxGridView 在遍历的时候有很多地方和gridview是不同的,对于和我一样对ASPxGridView 不属性的人来说,要花很多的事件去找和适当属性。
其实ASPxGridView 官方也提供了实现全选的例子不过那个似乎是不能绑定数据库的。
遍历ASPxGridView 时findcontrol事要注意要用 (CheckBox)ASPxGridView1.FindRowCellTemplateControl(i, (GridViewDataColumn)ASPxGridView1.Columns["SelectAll"], "CheckBox1")来取得某行的CheckBox
还有记得要把head的CheckBox的设置AutoPostBack="True" ,负责当点击CheckBox时相当与head的click。
下面是详细代码:
cs
代码
aspx文件
代码
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="结合checkbox_Default" %>
<%@ Register assembly="DevExpress.Web.ASPxGridView.v7.3, Version=7.3.7.0, Culture=neutral, PublicKeyToken=6d2d307aee1dfbe0" namespace="DevExpress.Web.ASPxGridView" tagprefix="dxwgv" %>
<%@ Register assembly="DevExpress.Web.ASPxEditors.v7.3, Version=7.3.7.0, Culture=neutral, PublicKeyToken=6d2d307aee1dfbe0" namespace="DevExpress.Web.ASPxEditors" tagprefix="dxe" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<dxwgv:ASPxGridView ID="ASPxGridView1" runat="server"
AutoGenerateColumns="False" DataSourceID="SqlDataSource1"
KeyFieldName="ProductID">
<Columns>
<dxwgv:GridViewDataTextColumn FieldName="ProductID" ReadOnly="True"
VisibleIndex="0">
<EditFormSettings Visible="False" />
</dxwgv:GridViewDataTextColumn>
<dxwgv:GridViewDataTextColumn FieldName="ProductName" VisibleIndex="1">
</dxwgv:GridViewDataTextColumn>
<dxwgv:GridViewDataCheckColumn FieldName="Discontinued" VisibleIndex="2">
</dxwgv:GridViewDataCheckColumn>
<dxwgv:GridViewDataTextColumn FieldName="SelectAll" VisibleIndex="3">
<DataItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server"
Checked='<%# Eval("Discontinued") %>' />
</DataItemTemplate>
<HeaderTemplate>
<asp:CheckBox ID="selectAll" runat="server" AutoPostBack="True"
oncheckedchanged="selectAll_CheckedChanged" />
SelectAll
</HeaderTemplate>
</dxwgv:GridViewDataTextColumn>
</Columns>
</dxwgv:ASPxGridView>
</div>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
SelectCommand="SELECT [ProductID], [ProductName], [Discontinued] FROM [Products] ORDER BY [ProductID]">
</asp:SqlDataSource>
</form>
</body>
</html>
<%@ Register assembly="DevExpress.Web.ASPxGridView.v7.3, Version=7.3.7.0, Culture=neutral, PublicKeyToken=6d2d307aee1dfbe0" namespace="DevExpress.Web.ASPxGridView" tagprefix="dxwgv" %>
<%@ Register assembly="DevExpress.Web.ASPxEditors.v7.3, Version=7.3.7.0, Culture=neutral, PublicKeyToken=6d2d307aee1dfbe0" namespace="DevExpress.Web.ASPxEditors" tagprefix="dxe" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<dxwgv:ASPxGridView ID="ASPxGridView1" runat="server"
AutoGenerateColumns="False" DataSourceID="SqlDataSource1"
KeyFieldName="ProductID">
<Columns>
<dxwgv:GridViewDataTextColumn FieldName="ProductID" ReadOnly="True"
VisibleIndex="0">
<EditFormSettings Visible="False" />
</dxwgv:GridViewDataTextColumn>
<dxwgv:GridViewDataTextColumn FieldName="ProductName" VisibleIndex="1">
</dxwgv:GridViewDataTextColumn>
<dxwgv:GridViewDataCheckColumn FieldName="Discontinued" VisibleIndex="2">
</dxwgv:GridViewDataCheckColumn>
<dxwgv:GridViewDataTextColumn FieldName="SelectAll" VisibleIndex="3">
<DataItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server"
Checked='<%# Eval("Discontinued") %>' />
</DataItemTemplate>
<HeaderTemplate>
<asp:CheckBox ID="selectAll" runat="server" AutoPostBack="True"
oncheckedchanged="selectAll_CheckedChanged" />
SelectAll
</HeaderTemplate>
</dxwgv:GridViewDataTextColumn>
</Columns>
</dxwgv:ASPxGridView>
</div>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
SelectCommand="SELECT [ProductID], [ProductName], [Discontinued] FROM [Products] ORDER BY [ProductID]">
</asp:SqlDataSource>
</form>
</body>
</html>
数据库有的是Nothwind表是Products