CS source
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections;
using System.Collections.Generic;
using DevExpress.Web;
using DevExpress.Web.ASPxDataControls;
using DevExpress.Web.ASPxGrid;
public partial class _Default: System.Web.UI.Page {
//Data Variables.
protected DataSet DataSet1 = new DataSet();
protected DataTable DataTable1 = new DataTable();
protected DataColumn DataColumn1 = new DataColumn();
protected DataColumn DataColumn2 = new DataColumn();
public List<string> SelectedRows {
get {
List<string> _selectedRows = Session["SelectedRows"] as List<string>;
if (_selectedRows == null) {
_selectedRows = new List<string>();
Session["SelectedRows"] = _selectedRows;
}
return _selectedRows;
}
}
protected void Page_Init(object sender, EventArgs e) {
//Initialize datasets
DataSet1.DataSetName = "NewDataSet";
DataSet1.Locale = new System.Globalization.CultureInfo("en-US");
DataSet1.Tables.AddRange(new DataTable[] { DataTable1 });
DataTable1.Columns.AddRange(new DataColumn[] { DataColumn1, DataColumn2 });
DataTable1.TableName = "Table1";
DataColumn1.ColumnName = "ID";
DataColumn1.DataType = typeof(int);
DataColumn2.ColumnName = "Name";
DataSet1.EndInit();
DataTable1.EndInit();
}
protected void Page_Load(object sender, EventArgs e) {
ASPxGrid1.DataSource = DataSet1;
ASPxGrid1.DataKeyField = "ID";
DataBind();
}
protected override void RaisePostBackEvent(IPostBackEventHandler sourceControl, string eventArgument) {
// the onclick="event.cancelBubble=true;" attribute is added to the template checkbox to
// prevent enter to edit mode on checkbox click
foreach (ASPxGridItem item in ASPxGrid1.Items) {
if (item.ItemType == ItemType.Item || item.ItemType == ItemType.AlternatingItem) {
bool checkedState;
CheckBox currentCheckbox = item.FindControl("CheckBox1") as CheckBox;
checkedState = currentCheckbox.Checked;
if (checkedState) {
if (!IsRowSelected(item.Row.KeyValue.ToString()))
SelectedRows.Add(item.Row.KeyValue.ToString());
}
else
SelectedRows.Remove(item.Row.KeyValue.ToString());
}
}
base.RaisePostBackEvent(sourceControl, eventArgument);
}
protected void ASPxGrid1_FetchingRows(object sender, EventArgs e) {
// this line need if the ASPxGrid1.DataBind(Rebind.Yes) is called
if (DataTable1.Rows.Count == 0) {
for (int i = 0; i < 10; i++) {
DataTable1.Rows.Add(i, "Item " + i.ToString());
}
}
}
public bool IsRowSelected(string keyValue) {
return SelectedRows.IndexOf(keyValue) > -1;
}
protected void Button1_Click(object sender, EventArgs e) {
SelectedRows.Clear();
foreach (DataRow row in DataTable1.Rows) {
if (!IsRowSelected(row[0].ToString()))
SelectedRows.Add(row[0].ToString());
}
ASPxGrid1.DataBind(Rebind.Yes); // in VS2005 the DataBind(true) has different meaning (see the documentation)
}
protected void Button2_Click(object sender, EventArgs e) {
ListBox1.Items.Clear();
foreach (string key in SelectedRows) {
ListBox1.Items.Add(key);
}
}
protected void ASPxGrid1_ItemDataBound(object sender, ItemEventArgs e) {
if (e.Item.ItemType == ItemType.Item || e.Item.ItemType == ItemType.AlternatingItem) {
CheckBox currentCheckbox = e.Item.FindControl("CheckBox1") as CheckBox;
currentCheckbox.Checked = IsRowSelected(e.Item.Row.KeyValue.ToString());
}
}
protected void Button2_Click(object sender, EventArgs e)
{
}
}
html source
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<%@ Register Assembly="DevExpress.Web.ASPxGrid.v7.1, Version=7.1.1.0, Culture=neutral, PublicKeyToken=79868b8147b5eae4"
Namespace="DevExpress.Web.ASPxGrid" TagPrefix="dxwg" %>
<%@ Register Assembly="DevExpress.Web.ASPxDataControls.v7.1, Version=7.1.1.0, Culture=neutral, PublicKeyToken=79868b8147b5eae4"
Namespace="DevExpress.Web.ASPxDataControls" TagPrefix="dxwdc" %>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<dxwg:ASPxGrid ID="ASPxGrid1" runat="server" OnFetchingRows="ASPxGrid1_FetchingRows" AutoGenerateColumns="False" TabIndex="0" OnItemDataBound="ASPxGrid1_ItemDataBound" ProcessingMode="Server">
<FooterStyle FixedHeight="True" FixedWidth="True" />
<StatusBars>
<dxwg:StatusBar Height="20px" StatusBarType="Regular">
<BarItems>
<dxwdc:BarStatusSection StatusSectionType="Status">
</dxwdc:BarStatusSection>
<dxwdc:BarStatusSection StatusSectionType="VisibleInterval">
</dxwdc:BarStatusSection>
<dxwdc:BarStatusSection StatusSectionType="TotalVisible">
</dxwdc:BarStatusSection>
<dxwdc:BarStatusSection StatusSectionType="TotalRows">
</dxwdc:BarStatusSection>
</BarItems>
</dxwg:StatusBar>
</StatusBars>
<GroupItemStyle FixedWidth="True" Wrap="False">
</GroupItemStyle>
<HeaderStyle FixedHeight="True" FixedWidth="True" Wrap="False" />
<ButtonBars>
<dxwg:ButtonBar ButtonBarType="Navigator">
<BarItems>
<dxwdc:BarButton ButtonType="MoveFirst">
</dxwdc:BarButton>
<dxwdc:BarButton ButtonType="MovePrevPage">
</dxwdc:BarButton>
<dxwdc:BarButton ButtonType="MovePrev">
</dxwdc:BarButton>
<dxwdc:BarTwoStateEditorButton ButtonType="ChangePageSize">
</dxwdc:BarTwoStateEditorButton>
<dxwdc:BarButton ButtonType="MoveNext">
</dxwdc:BarButton>
<dxwdc:BarButton ButtonType="MoveNextPage">
</dxwdc:BarButton>
<dxwdc:BarButton ButtonType="MoveLast">
</dxwdc:BarButton>
<dxwdc:BarButton ButtonType="InsertRow">
</dxwdc:BarButton>
<dxwdc:BarButton ButtonType="EditRow">
</dxwdc:BarButton>
<dxwdc:BarButton ButtonType="DeleteRow">
</dxwdc:BarButton>
<dxwdc:BarButton ButtonType="Refresh">
</dxwdc:BarButton>
<dxwdc:BarEditModeButton ButtonType="Post">
</dxwdc:BarEditModeButton>
<dxwdc:BarEditModeButton ButtonType="Cancel">
</dxwdc:BarEditModeButton>
</BarItems>
</dxwg:ButtonBar>
</ButtonBars>
<SearchBtnStyle FixedWidth="True">
</SearchBtnStyle>
<Columns>
<dxwg:BoundColumn DataField="ID" HeaderText="ID" VisibleIndex="0">
</dxwg:BoundColumn>
<dxwg:BoundColumn DataField="Name" HeaderText="Name" VisibleIndex="1">
</dxwg:BoundColumn>
<dxwg:TemplateColumn HeaderText="Selection" VisibleIndex="2">
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" onclick="event.cancelBubble=true;" />
</ItemTemplate>
</dxwg:TemplateColumn>
</Columns>
<ItemStyle FixedWidth="True" Wrap="False" />
</dxwg:ASPxGrid>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Select All" />
</div>
<asp:Button ID="Button2" runat="server" OnClick="Button2_Click" Text="GetSelected" /><br />
<asp:ListBox ID="ListBox1" runat="server" Height="168px" Width="264px"></asp:ListBox>
</form>
</body>
</html>