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

Select the page of the selected item

5 Answers 150 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Kris Guillaumier
Top achievements
Rank 1
Kris Guillaumier asked on 22 Aug 2008, 09:47 AM
I am selecting a particular item programatically after i rebind. Now i want to set the CurrentPageIndex of the item I have just programatically selected. Is there any way i can do this?

Something like this:

RadGrid1.Rebind();

foreach (GridItem item in RadGrid1.Items)

{

if (item.OwnerTableView.DataKeyValues[item.ItemIndex]["ID"].ToString() == ID)

{

item.Selected =

true;

// Now i need something like :  RadGrid1.CurrentPageIndex = item.PageIndex;

break;

}

}



Thanks

5 Answers, 1 is accepted

Sort by
0
Nikita Gourme
Top achievements
Rank 1
answered on 22 Aug 2008, 12:17 PM
I can think of two ways doing what you describe:
  • disable the paging for the grid temporary (AllowPaging = false and Rebind() call after that)
  • find and select the item of your choice
  • determine the page to switch the page index by dividing the entire set of data by the PageSize set and detect the page to which the item belongs using its index

    or
  • find directly the item in the underlying data source
  • perform the same calculations to identify on which page it resides and change the page index to make that page active
Hope this helps.

Nikita
0
Kris Guillaumier
Top achievements
Rank 1
answered on 18 Sep 2008, 09:53 AM
I opted for the 2nd option to find the item directly in the underlying data source and calculated the page index.$0$0$0$0To do this i handled the selected event of the object datasource which will contain all rows in one of its arguments. Calcalating the right page index is successful however, simply setting the CurrentPageIndex is not enough. I have to rebind after setting the CurrentPageIndex but this will have an impact on performance as i have to rebind twice, 1st time to refresh the data, and 2nd time to selec the right page and item.$0
0
Sebastian
Telerik team
answered on 18 Sep 2008, 10:06 AM
Hello Kris,

Indeed there will be two selects performed  - one initially and one after you modify the current page index (based on the custom calculations) and rebind the grid. The second binding is necessary to reflect the page index change. I hope that the second binding is not a show stopper for you since there is no other alternative available.

Kind regards,
Stephen
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
Alexis
Top achievements
Rank 1
answered on 20 Nov 2008, 06:47 PM
Hello,
I've a solution that only requires 1 rebind.
The trick is to find the right PageIndex of the item that you need to select in the NeedDataSource of the grid. You then need to select that item in the dataBinding.

Here's my code:

protected void GridCategorie_NeedDataSource(object source, Telerik.Web.UI.GridNeedDataSourceEventArgs e)  
    {  
        Database db = DatabaseFactory.CreateDatabase();  
        using (DbConnection connection = db.CreateConnection())  
        {  
            try  
            {  
                connection.Open();  
                using (DbTransaction tran = connection.BeginTransaction())  
                {  
                    DbCommand cmdEncaissements = db.GetSqlStringCommand("SELECT PnpIDCategorie, NomCategorieFr FROM webSite.Categorie");  
                    DataSet myDataTable = db.ExecuteDataSet(cmdEncaissements, tran);  
 
                    if (!isGridPagerChangedIndex)  
                    {  
                        int compteur = 1;  
                        int itemPageIndex = 0;  
                        int PageSize = ((RadGrid)source).MasterTableView.PageSize;  
                        foreach (DataRow dr in myDataTable.Tables[0].Rows)  
                        {  
                            if (string.Equals(dr["PnpIDCategorie"].ToString(), categorieID))  
                            {  
                                break;  
                            }  
                            if (compteur == PageSize)  
                            {  
                                itemPageIndex++;  
                                compteur = 1;  
                            }  
                            else  
                            {  
                                compteur++;  
                            }  
                        }  
                        ((RadGrid)source).MasterTableView.CurrentPageIndex = itemPageIndex;  
                    }  
                    ((RadGrid)source).DataSource = myDataTable;  
                    isGridPagerChangedIndex = false;  
                }  
            }  
            catch (Exception ex)  
            {  
                Logger.Write(ex.Message, LogType.Error, LogPriority.Critical);  
            }  
        }  
    }  
 
protected void GridCategorie_ItemDataBound(object sender, GridItemEventArgs e)  
    {  
        if ((e.Item.ItemType == GridItemType.AlternatingItem) || (e.Item.ItemType == GridItemType.Item))  
        {  
            if (string.Equals(e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["PnpIDCategorie"].ToString(), categorieID))  
            {  
                ((GridDataItem)e.Item).Selected = true;  
            }  
        }  
    }  
 

The variable categorieID is the ID for the item that I need to select. I assigned it in the pageLoad since i received as a URL argument.
The variable isGridPagerChangedIndex is a boolean that still let me use my grid pager to switch the CurrentPageIndex.

I hope it helps.

Alexis
0
Sebastian
Telerik team
answered on 21 Nov 2008, 08:51 AM
Hi Alexis,

Thank you for posting your solution in our public forums. Thus other community members can benefit from it as well. I updated your Telerik points for the involvement.

Kind regards,
Stephen
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
Tags
Grid
Asked by
Kris Guillaumier
Top achievements
Rank 1
Answers by
Nikita Gourme
Top achievements
Rank 1
Kris Guillaumier
Top achievements
Rank 1
Sebastian
Telerik team
Alexis
Top achievements
Rank 1
Share this question
or