Articles in this section
Category / Section

How to get selected rows data in code behind in .NET WebForms Grid?

1 min read

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.


Note:

A new version of Essential Studio for ASP.NET is available. Versions prior to the release of Essential Studio 2014, Volume 2 will now be referred to as a classic versions.The new ASP.NET suite is powered by Essential Studio for JavaScript providing client-side rendering of HTML 5-JavaScript controls, offering better performance, and better support for touch interactivity. The new version includes all the features of the old version, so migration is easy.

The Classic controls can be used in existing projects; however, if you are starting a new project, we recommend using the latest version of Essential Studio for ASP.NET. Although Syncfusion will continue to support all Classic Versions, we are happy to assist you in migrating to the newest edition.

For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls. If you have any queries or require clarifications, please let us know in the comments section below.

You can also contact us through our support forumsDirect-Trac, or feedback portal. We are always happy to assist you!

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments
Please sign in to leave a comment
Access denied
Access denied