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

Set CurrentPageIndex Based on Item's Page

1 Answer 98 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Robert
Top achievements
Rank 1
Robert asked on 17 Apr 2009, 01:01 AM
I have a RadGrid which binds to a dataset. I'd like to programatically set an item as selected/expanded, and have the grid automatically set the CurrentPageIndex to the correct page number to show the selected/expanded item.

Is this possible? My code works great as long as the item is on the first page.

1 Answer, 1 is accepted

Sort by
0
Accepted
Princy
Top achievements
Rank 2
answered on 17 Apr 2009, 06:03 AM
Hello Robert,

To select/expand a data item in the grid you can use the following code:
c#:
protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e) 
    { 
        if (e.Item is GridDataItem) 
        { 
            GridDataItem dataItem = (GridDataItem)e.Item; 
            if (dataItem["ID"].Text == "14"
            {  
              dataItem.Selected = true
              dataItem.Expanded = true
            } 
        } 
    } 

To search for an item based on some criteria, and set it as selected and set the grids page index as the selected item's page index you can use the following code in the PreRender event of the grid or a button click event for instance:
c#:
    int count = RadGrid1.MasterTableView.PageCount; 
    int flag = 0; 
    for (int i = 0; i < count; i++) 
    { 
        RadGrid1.CurrentPageIndex = i; 
        RadGrid1.Rebind(); 
        foreach(GridDataItem item in RadGrid1.MasterTableView.Items) 
        { 
            if (item.GetDataKeyValue("ID").ToString() == "12"
            { 
                item.Selected = true
                item.Expanded= true;
                flag = 1; 
                break
            } 
        } 
        if (flag == 1) 
            break
    }  

Thanks
Princy.
Tags
Grid
Asked by
Robert
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Share this question
or