We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

How to get selected rows data in code behind?

Platform: ASP.NET Web Forms |
Control: Grid
Tags: selection

This explains the way of how to get the selected records data in the server side

 

Solution:

       To get the selected row index in server side (code behind) use SelectedRowIndex API and using the index get the selected row data from the dataSource based on the current page.

ASPX

<asp:Button ID="button" runat="server" Text="SelectedRow" OnClick="button_Click" />
 
      <ej:Grid ID="FlatGrid" runat="server" AllowPaging="True">
            <Columns>
                <ej:Column Field="OrderID" HeaderText="Order ID" IsPrimaryKey="True" TextAlign="Right" Width="75" />
                <ej:Column Field="CustomerID" HeaderText="Customer ID" Width="80" />
                <ej:Column Field="EmployeeID" HeaderText="Employee ID" TextAlign="Right" Width="75"/>
                <ej:Column Field="Freight" HeaderText="Freight" TextAlign="Right" Width="75" Format="{0:C}"/>
                <ej:Column Field="OrderDate" HeaderText="Order Date" TextAlign="Right" Width="80" Format="{0:MM/dd/yyyy}"/>
                <ej:Column Field="ShipCity" HeaderText="Ship City" Width="110" />
            </Columns>
        </ej:Grid>
    

 

C#

  protected void button_Click(object sender, EventArgs e)
        {
            var index = this.FlatGrid.SelectedRowIndex;     //get the index of the selected row in current
            List<Orders> data = ViewState["DataSource"] as List<Orders>;
            if(this.FlatGrid.PageSettings.CurrentPage > 1)
               index = this.FlatGrid.PageSettings.PageSize * (this.FlatGrid.PageSettings.CurrentPage - 1) + index;        //get index to retrieve the data from the dataSource based on the pagesize and currenpage
            var selectedData = data[index];    //selectedData contains the current selected Row data
        } 
     }
  }

 

 

Here we can get the selected records in grid based on the pageSize and current page.

 

shows the record has selected

 

Figure 1: shows the record has selected on page 3.

 

 shows the selected row data

 

Figure 2: shows the selected row data in code behind.

2X faster development

The ultimate ASP.NET Web Forms UI toolkit to boost your development speed.
ADD COMMENT
You must log in to leave a comment
Comments
Hercules
Jul 24, 2018

This does work 100% if there is no filter on the original Grid.

How will it be done if there is a filter applied?

Reply
Kuralarasan Muthusamy [Syncfusion]
Jul 25, 2018

Hi Hercules,

 

From your given query, we found that you want to get the selected records in server side after the grid filtering. In this kb we have displayed the selected records from the grid dataSource by using selectedrowIndex. But in your cause, we suggest you to use filtered records instead of this grid dataSource to get the selected record. Because after the grid filtering selected row index was changed based on filtered records position. So you need to use this filtered records instead of the dataSource to get the selected records. We used DataOperations to get the filtered records in server side. You need to pass the grid dataSource and grid model to the dataOperations execute method to get the filtered records. Please refer the following code snippet;

 

        protected void button_Click(object sender, EventArgs e)

        {

            var index = this.OrdersGrid.SelectedRowIndex;    //get the index of the selected row in current

            DataOperations ds = new DataOperations();

            var data1 = ds.Execute(order, this.OrdersGrid.Model); // passed the grid dataSource and grid dataSource in DataOperations execute method

            if (this.OrdersGrid.PageSettings.CurrentPage > 1)

                index = this.OrdersGrid.PageSettings.PageSize * (this.OrdersGrid.PageSettings.CurrentPage - 1) + index;

            var selectedRecord = data1.Cast<Orders>().ToList()[index]; // get the selected records from the filtered records

        }


In this code we have used the filtered records to get the selected record in server side.

 

Also we have attached our sample for your reference and that sample can be downloadable from the below link,

 

Sample: http://www.syncfusion.com/downloads/support/directtrac/general/ze/sample1197375515.zip

 

Regards,

Kuralarasan M.

Reply

Please sign in to access our KB

This page will automatically be redirected to the sign-in page in 10 seconds.

Up arrow icon

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

Live Chat Icon For mobile