Move to page that matches a given datafield value

3 posts, 0 answers
  1. Peter
    Peter avatar
    4 posts
    Member since:
    Jul 2015

    Posted 24 Jul 2015 Link to this post

    I have a datasource that has an ID column.  I'd like to display the page with the row that matches a [user-provided] ID.  This could also be done two fold by (a) querying which page a particular datafield value is on; (b) displaying a particular page
  2. Konstantin Dikov
    Admin
    Konstantin Dikov avatar
    2466 posts

    Posted 27 Jul 2015 Link to this post

    Hello Peter,

    For finding the page holding the item with a given ID you can either temporarily disable the paging functionality, find the index of the item with that ID and calculate the page index. Afterwords you can specify the RadGrid's CurrentPageIndex property and rebind the grid.

    Another option would be to traverse each page of the grid until you find the item in question:
    <telerik:RadNumericTextBox runat="server" ID="RadNumericTextBox1" Value="27"></telerik:RadNumericTextBox>
    <telerik:RadButton runat="server" ID="RadButton1" Text="Find the page with the following ID" OnClick="RadButton1_Click"></telerik:RadButton>
     
    <telerik:RadGrid runat="server" ID="RadGrid1" OnNeedDataSource="RadGrid1_NeedDataSource" AllowPaging="true">
        <MasterTableView DataKeyNames="ID">
        </MasterTableView>
    </telerik:RadGrid>

    And the code-behind:
    protected void RadGrid1_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
    {
        DataTable table = new DataTable();
        table.Columns.Add("ID", typeof(int));
        table.Columns.Add("FirstName", typeof(string));
        table.Columns.Add("LastName", typeof(string));
        table.Columns.Add("Age", typeof(int));
        table.Columns.Add("Date", typeof(DateTime));
        table.Columns.Add("BoolValue", typeof(Boolean));
        for (int i = 0; i < 50; i++)
        {
            table.Rows.Add(i, "FirstName" + i, "LastName" + i, 20 + i, DateTime.Now.AddDays(i), i % 2 == 0);
        }
     
        (sender as RadGrid).DataSource = table;
    }
     
    protected void RadButton1_Click(object sender, EventArgs e)
    {
        string searchedID = RadNumericTextBox1.Value.ToString();
        int pageIndex = 0;
        bool itemFound = false;
        int originalIndex = RadGrid1.CurrentPageIndex;
     
        do
        {
            RadGrid1.CurrentPageIndex = pageIndex++;
            RadGrid1.Rebind();
            foreach (GridDataItem item in RadGrid1.Items)
            {
                if (item.GetDataKeyValue("ID").ToString() == searchedID)
                {
                    itemFound = true;
                }
            }
        } while (!itemFound && RadGrid1.CurrentPageIndex < RadGrid1.PageCount - 1);
     
        if (!itemFound)
        {
            RadGrid1.CurrentPageIndex = originalIndex;
            RadGrid1.Rebind();
        }
    }

    Hope this helps.


    Regards,
    Konstantin Dikov
    Telerik
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
  3. Peter
    Peter avatar
    4 posts
    Member since:
    Jul 2015

    Posted 28 Jul 2015 in reply to Konstantin Dikov Link to this post

    Thanks - I'll check it out.  I wasn't aware of the CurrentPageIndex property.
Back to Top