Example E2625
Visible to All Users

Grid View for ASP.NET Web Forms - How to use ASPxListBox to edit grid data inside the grid edit form

This example shows how to use the ASPxListBox control inside the edit form.

Files to Review

Example Code

Default.aspx
ASPx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> <%@ Register Assembly="DevExpress.Web.v15.1, Version=15.1.15.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a" Namespace="DevExpress.Web" TagPrefix="dx" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title>How to use ASPxListBox inside the EditForm for editing ASPxGridView</title> </head> <body> <form id="form1" runat="server"> <div> <h1>How to use ASPxListBox inside the EditForm for editing ASPxGridView</h1> <dx:ASPxGridView ID="ASPxGridView1" runat="server" AutoGenerateColumns="False" DataSourceID="AccessDataSource1" KeyFieldName="CategoryID" OnRowUpdating="ASPxGridView1_RowUpdating" OnRowValidating="ASPxGridView1_RowValidating"> <Columns> <dx:GridViewCommandColumn VisibleIndex="0" ShowEditButton="true"> </dx:GridViewCommandColumn> <dx:GridViewDataTextColumn FieldName="CategoryID" ReadOnly="True" VisibleIndex="0"> <EditFormSettings Visible="False" /> </dx:GridViewDataTextColumn> <dx:GridViewDataTextColumn FieldName="CategoryName" VisibleIndex="1"> </dx:GridViewDataTextColumn> <dx:GridViewDataTextColumn FieldName="Description" VisibleIndex="2"> </dx:GridViewDataTextColumn> </Columns> <Templates> <EditForm> <dx:ASPxListBox ID="ASPxListBox1" runat="server" DataSourceID="AccessDataSource2" SelectionMode="CheckColumn" TextField="ProductName" ValueField="ProductName" ValueType="System.String" OnDataBound="ASPxListBox1_DataBound"> </dx:ASPxListBox> <br /> <dx:ASPxGridViewTemplateReplacement ID="btnUpdate" runat="server" ReplacementType="EditFormUpdateButton"></dx:ASPxGridViewTemplateReplacement> <dx:ASPxGridViewTemplateReplacement ID="btnCancel" runat="server" ReplacementType="EditFormCancelButton"></dx:ASPxGridViewTemplateReplacement> </EditForm> </Templates> </dx:ASPxGridView> </div> <asp:AccessDataSource ID="AccessDataSource1" runat="server" DataFile="~/App_Data/nwind.mdb" SelectCommand="SELECT [CategoryID], [CategoryName], [Description] FROM [Categories]" DeleteCommand="DELETE FROM [Categories] WHERE [CategoryID] = ?" InsertCommand="INSERT INTO [Categories] ([CategoryID], [CategoryName], [Description]) VALUES (?, ?, ?)" UpdateCommand="UPDATE [Categories] SET [CategoryName] = ?, [Description] = ? WHERE [CategoryID] = ?"> <DeleteParameters> <asp:Parameter Name="CategoryID" Type="Int32" /> </DeleteParameters> <UpdateParameters> <asp:Parameter Name="CategoryName" Type="String" /> <asp:Parameter Name="Description" Type="String" /> <asp:Parameter Name="CategoryID" Type="Int32" /> </UpdateParameters> <InsertParameters> <asp:Parameter Name="CategoryID" Type="Int32" /> <asp:Parameter Name="CategoryName" Type="String" /> <asp:Parameter Name="Description" Type="String" /> </InsertParameters> </asp:AccessDataSource> <asp:AccessDataSource ID="AccessDataSource2" runat="server" DataFile="~/App_Data/nwind.mdb" SelectCommand="SELECT [ProductName] FROM [Products]"></asp:AccessDataSource> </form> </body> </html>
Default.aspx.cs
C#
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using DevExpress.Web; public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } protected void ASPxListBox1_DataBound(object sender, EventArgs e) { var listBox = (ASPxListBox)sender; int editingRowVisibleIndex = ASPxGridView1.EditingRowVisibleIndex; string rowValue = ASPxGridView1.GetRowValues(editingRowVisibleIndex, "Description").ToString(); string[] rowValueItems = rowValue.Split(';'); List<string> rowValueItemsAsList = new List<string>(); rowValueItemsAsList.AddRange(rowValueItems); foreach (ListEditItem item in listBox.Items) { if (rowValueItemsAsList.Contains(item.Value.ToString())) { item.Selected = true; } } } protected void ASPxGridView1_RowUpdating(object sender, DevExpress.Web.Data.ASPxDataUpdatingEventArgs e) { var grid = (ASPxGridView)sender; ASPxListBox listBox = (ASPxListBox)grid.FindEditFormTemplateControl("ASPxListBox1"); string selectedItemsAsString = string.Empty; foreach (ListEditItem item in listBox.SelectedItems) { selectedItemsAsString += item.Text + ";"; } if (selectedItemsAsString.Length > 0) { selectedItemsAsString = selectedItemsAsString.Trim(';'); } // e.NewValues["Description"] = selectedItemsAsString; //Uncomment this line to allow editing } protected void ASPxGridView1_RowValidating(object sender, DevExpress.Web.Data.ASPxDataValidationEventArgs e) { e.RowError = "Data source modification is not allowed. To test the updating operation, download the project and run it locally";//Remove this line in local environment } }

Disclaimer: The information provided on DevExpress.com and affiliated web properties (including the DevExpress Support Center) is provided "as is" without warranty of any kind. Developer Express Inc disclaims all warranties, either express or implied, including the warranties of merchantability and fitness for a particular purpose. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.

Confidential Information: Developer Express Inc does not wish to receive, will not act to procure, nor will it solicit, confidential or proprietary materials and information from you through the DevExpress Support Center or its web properties. Any and all materials or information divulged during chats, email communications, online discussions, Support Center tickets, or made available to Developer Express Inc in any manner will be deemed NOT to be confidential by Developer Express Inc. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.