This is a migrated thread and some comments may be shown as answers.

Move to page that matches a given datafield value

2 Answers 8 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Peter
Top achievements
Rank 1
Peter asked on 24 Jul 2015, 11:31 PM
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 Answers, 1 is accepted

Sort by
0
Konstantin Dikov
Telerik team
answered on 27 Jul 2015, 12:37 PM
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
0
Peter
Top achievements
Rank 1
answered on 28 Jul 2015, 03:39 PM
Thanks - I'll check it out.  I wasn't aware of the CurrentPageIndex property.
Tags
Grid
Asked by
Peter
Top achievements
Rank 1
Answers by
Konstantin Dikov
Telerik team
Peter
Top achievements
Rank 1
Share this question
or