Example E319
Visible to All Users

Grid View for ASP.NET Web Forms - How to get column values of multiple selected rows.

This example demonstrates how to use the server-side GetSelectedFieldValues method to obtain field values of multiple selected rows.

GetSelectedFieldValues

Overview

Follow the steps below:

  1. Set the grid's AllowSelectByRowClick property to true to enable row selection.
    C#
    protected void Page_Init(object sender, EventArgs e) { // ... ASPxGridView1.SettingsBehavior.AllowSelectByRowClick = true; // ... }
  2. In a button's Click event handler, call the GetSelectedValues function. In this function, get the field names of grid columns and call the GetSelectedFieldValues method to obtain the field values of selected rows.
    C#
    protected void ASPxButton1_Click(object sender, EventArgs e) { GetSelectedValues(); // ... } List<object> selectedValues; private void GetSelectedValues() { List<string> fieldNames = new List<string>(); foreach(GridViewColumn column in ASPxGridView1.Columns) if(column is GridViewDataColumn) fieldNames.Add(((GridViewDataColumn)column).FieldName); selectedValues = ASPxGridView1.GetSelectedFieldValues(fieldNames.ToArray()); }

Files to Review

Documentation

Does this example address your development requirements/objectives?

(you will be redirected to DevExpress.com to submit your response)

Example Code

GetSelectedValuesAllColumns/Default.aspx
ASPx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="GetSelectedValuesAllColumns._Default" %> <%@ Register Assembly="DevExpress.Web.v24.2, Version=24.2.2.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a" Namespace="DevExpress.Web" TagPrefix="dx" %> <!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> <dx:ASPxGridView ID="ASPxGridView1" runat="server"> </dx:ASPxGridView> <dx:ASPxButton ID="ASPxButton1" runat="server" OnClick="ASPxButton1_Click" Text="Get Selected Values"> </dx:ASPxButton> </div> <asp:Literal ID="Literal1" runat="server"></asp:Literal> </form> </body> </html>
GetSelectedValuesAllColumns/Default.aspx.cs(vb)
C#
using System; using System.Data; using System.Configuration; using System.Collections; 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.Generic; using DevExpress.Web; namespace GetSelectedValuesAllColumns { public partial class _Default : System.Web.UI.Page { protected void Page_Init(object sender, EventArgs e) { ASPxGridView1.DataSource = GetData(); ASPxGridView1.KeyFieldName = "ID"; ASPxGridView1.SettingsBehavior.AllowSelectByRowClick = true; ASPxGridView1.DataBind(); if(!IsPostBack && !IsCallback) { ASPxGridView1.Columns[ASPxGridView1.KeyFieldName].Visible = false; } } private DataTable GetData() { DataTable table = new DataTable(); table.Columns.Add("ID", typeof(int)); table.Columns.Add("Name", typeof(string)); table.Columns.Add("Date", typeof(DateTime)); table.Columns.Add("IsActive", typeof(bool)); for(int i = 0; i < 10; i++) { table.Rows.Add(i, "Item " + i.ToString(), DateTime.Now.AddDays(i), i % 2 == 0); } return table; } protected void ASPxButton1_Click(object sender, EventArgs e) { GetSelectedValues(); PrintSelectedValues(); } List<object> selectedValues; private void GetSelectedValues() { List<string> fieldNames = new List<string>(); foreach(GridViewColumn column in ASPxGridView1.Columns) if(column is GridViewDataColumn) fieldNames.Add(((GridViewDataColumn)column).FieldName); selectedValues = ASPxGridView1.GetSelectedFieldValues(fieldNames.ToArray()); } private void PrintSelectedValues() { if(selectedValues == null) return; string result = ""; foreach(object[] item in selectedValues) { foreach(object value in item) { result += string.Format("{0}&nbsp;&nbsp;&nbsp;&nbsp;", value); } result += "</br>"; } Literal1.Text = result; } } }

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.