FindItemByKeyValue into several pages

10 posts, 0 answers
  1. Etienne
    Etienne avatar
    73 posts
    Member since:
    Apr 2005

    Posted 26 Jun 2008 Link to this post

    Hello,
    I bind a grid with many employees items. I tried to select the employee whose the key is mentionned in querystring : &EmployeeID=245.
    If all items appear in one page, FindItemByKeyValue("EmployeeID",245)  returns my item. But if I display items in several pages, the method returns nothing because it only searchs in the page rendered (which is page 1 and I don't know the page for employee 245).
    What is the best way to select the "key item" in multi pages without filtering records please ?
    Thank you.
  2. Etienne
    Etienne avatar
    73 posts
    Member since:
    Apr 2005

    Posted 27 Jun 2008 Link to this post

    Do I open a support ticket ?
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 27 Jun 2008 Link to this post

    Hi,

    Try the following code snippet in the PreRender event/

    CS:
     protected void RadGrid1_PreRender(object sender, EventArgs e) 
        { 
     
            foreach (GridDataItem item in RadGrid1.MasterTableView.Items) 
            { 
     
                string strtxt = item.GetDataKeyValue("EmployeeID").ToString(); 
                if (strtxt == "245") 
                { 
                    item.Selected = true
     
                } 
     
            } 
        } 


    Thanks
    Shinu.
  5. Etienne
    Etienne avatar
    73 posts
    Member since:
    Apr 2005

    Posted 27 Jun 2008 Link to this post

    Same problem than with FindItemByKeyValue method : key value is searched into the records rendered in the current grid page, not inside all pages of the grid.
    My first page contains employeeID from 1 to 10, and the 245 is not found and not selected (it may be in the 15th page).
    I can find the page by calculate the position of key value in my DataTable and divide it by grid PageCount, but if your component can find it automatically, it's better !
    So, do I need to calculate the page to render or is it a best solution please ?
  6. Sebastian
    Admin
    Sebastian avatar
    9934 posts

    Posted 30 Jun 2008 Link to this post

    Hi Etienne,

    My suggestion in this case is to traverse directly all underlying records from the grid source server-side and identify the record of interest by its key value. Some pointers about the approach you can find in this documentation topic:

    http://www.telerik.com/help/aspnet-ajax/grdtotalsingridfooters.html (paragraph
    Displaying totals for all grid pages)

    When you find the relevant item in the source, intercept the
    PreRender event of the grid, switch to the corresponding page (setting the CurrentPageIndex property of the grid), reference the grid row using the FindItemByKeyValue(keyname, keyvalue) method (as you already found out), set the Edit property of the row to true and refresh the grid invoking its Rebind() method.

    Best regards,
    Stephen
    the Telerik team

    Instantly find answers to your questions at the new Telerik Support Center
  7. Etienne
    Etienne avatar
    73 posts
    Member since:
    Apr 2005

    Posted 30 Jun 2008 Link to this post

    Yes, it's exactly what I did since my first post, and it's working great.
    So, I think it should be an interesting adding to the RadGrid to find an item by his key amoung all those of the grid besides those of the current page only.
    Thank you for this precision.
  8. Pablo Tola
    Pablo Tola avatar
    43 posts
    Member since:
    Nov 2007

    Posted 17 Jul 2008 Link to this post

    Could you post example code of your solution, I'm looking to do the same thing.

    thanks.
  9. Etienne
    Etienne avatar
    73 posts
    Member since:
    Apr 2005

    Posted 05 Aug 2008 Link to this post

    Sorry for late, I was in holidays...
    This is a part of my code wich allows to select grid line of a Primary Key passed in querystring

     

     
                    'select page where key value exists -> allow selecting row below  
                    Try  
                        PKValue = CInt(Request.QueryString(_PKName))  
                    Catch  
                        'querystring hacking or bad prog -> PKValue = -1  
                    End Try  
                    If PKValue > -1 Then  
                        For Each oRow As DataRow In DT.Rows  
                            IndexKeyValue += 1  
                            If CInt(oRow(_PKName)) = PKValue Then Exit For  
                        Next  
                        RadGrid1.CurrentPageIndex = IndexKeyValue \ RadGrid1.PageSize  
                    End If  
                    RadGrid1.DataBind()  
     
                    If Not Page.IsPostBack Then  
                        Dim it As GridItem = RadGrid1.MasterTableView.FindItemByKeyValue(_PKName, PKValue) 'can't work, find only in rendered page !  
                        If Not it Is Nothing Then  
                            it.Selected = True 
                            RaiseEvent SelectedIndexChanged(PKValue)  
                        End If  
                    End If 
  10. ivantkt
    ivantkt avatar
    14 posts
    Member since:
    Jan 2007

    Posted 20 Oct 2008 Link to this post

    Hello, thanks for the coments, you have tell us a good answer, but
    a i have the same problem.

    I do the solution you suggest, but there is another problem i can't solve.

    I do the netx steps:
    Find the relevant item in the source, intercept the PreRender event of the grid, switch to the corresponding page (setting the CurrentPageIndex property of the grid), reference the grid row using the FindItemByKeyValue() and select the item (item.Selected = true).

    But if i have a grid column sorted ASC, for example: I have a column (clmName) to display the name of an employee, and i sort ASC this column, after this i try to select the item by the steps metioned before, but i get the problem that the DataSource index of the element does not "MATCH", with the index of the grid.

    For example

    The datasource is this (in this order):
    EmployeeNumber    EmployeeName    Index
                1                    Zac                          0
                2                    Charli                       1
                3                    Mickey                     2
                4                    Donald                      3

    The Grid items sorted by  EmployeeName (in this order):
    EmployeeNumber    EmployeeName        Index
                2                    Charli                            0
                4                    Donald                          1
                3                    Mickey                          2
                1                    Zac                               3

    If i want to select Zac, i find the relevant item in the source, and it index is
    DataSourceIndexOfZac = 0;
    But does not "MATCH", whit the index display in the grid
    GridIndexOfZac = 3;


    Can you help Me with this???
  11. Yavor
    Admin
    Yavor avatar
    11 posts

    Posted 21 Oct 2008 Link to this post

    Hi ivantkt,

    Another option is to re-query the underlying datasource, and determine on which page/level the item would be.
    This is demonstrated in the code sample attached to this message.
    I hope it helps.

    Regards,
    Yavor
    the Telerik team

    Check out Telerik Trainer, the state of the art learning tool for Telerik products.
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017