简体   繁体   中英

How can I programatically find and expand a particular row in a Telerik RadGrid?

How can one achieve the following requirement?

Based on values in the queryString, the requested record must be expanded into its DetailView.

This example uses Entity Framework 6 for accessing data. This means the RadGrid's pageSize, pageIndex and sort information must persist within the call EF makes to the database.

Let's assume the following URL when you arrive at the page containing the RadGrid.

    http://localhost:61878/search?pageindex=8&orderid=2871517&pagesize=50
  1. The RadGrid declarative markup would look like this.

     <telerik:RadGrid ID="OrdersMasterGrid" runat="server" AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False" OnNeedDataSource="GridOnNeedDataSource" OnPreRender="GridPreRender"> 
  2. Then the PageLoad would look like this. This fires BEFORE the RadGrid's OnNeedDataSource event so it's important to set the correct pageindex and pagesize because that information will flow all the way to the database.

     protected void Page_Load(object sender, EventArgs e) { if (IsPostBack == false) { OrderID_QueryString = Request.QueryString.GetValue<string>("orderid"); OrdersMasterGrid.MasterTableView.CurrentPageIndex = Request.QueryString.GetValue<int>("pageindex"); OrdersMasterGrid.MasterTableView.PageSize = Request.QueryString.GetValue<int>("pagesize"); } 
  3. Programatically expanding the correct row takes place during the OnPreRender event.
    The GridPreRender eventHandler would look like:

     protected void GridPreRender(object sender, EventArgs e) { foreach (object gridDataItem in OrdersMasterGrid.MasterTableView.Items) { if (gridDataItem is GridDataItem) { GridDataItem gdi = gridDataItem as GridDataItem; if (gdi["OrderID"].Text == OrderID_QueryString) { gdi.Expanded = true; break; } } } } 

Conclusion: Although this example uses the queryString, it might be better to use ViewData, ViewBag or other transparent mechanisms. The pitfall of this approach is if the user decides to bookmark the page, it's going to stop working eventually when the requested orderid does not exist in the data returned by EF6 for the 8th page when pagesize is 50.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM