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

[Solved] Grid programmatic record selection

7 Answers 277 Views
GridView
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Inquisitor Jax
Top achievements
Rank 1
Inquisitor Jax asked on 07 Sep 2009, 02:40 PM
Is there an easy way to select records in the grid?
I have an implementation where I have navigation buttons, where upon I need to programmatically select a record on the grid depending on what button was pressed:
Here's what I have so far, but it seems that DataRecord has been marked as Obsolete.
Is there another way I should be doing this?
PS: The method must only navigate records that are visible on the grid. (ie: take filtering and grouping into account)

    public enum GridNavigationMethod
    {
        First,
        Previous,
        Next,
        Last
    }

    public static void SelectItem(this RadGridView grid, GridNavigationMethod navigationMethod)
        {
            DataRecord currentRecord = grid.ItemsControl.FindRecord(grid.SelectedItem) as DataRecord;
            int indexToSelect = 0; //default to selecting the first item
            if (currentRecord != null)
            {
                int currentIndex = grid.Records.IndexOf(currentRecord);
                switch (navigationMethod)
                {
                    case GridNavigationMethod.First:
                        indexToSelect = 0;
                        break;
                    case GridNavigationMethod.Previous:
                        indexToSelect = currentIndex > 0 ? currentIndex - 1 : currentIndex;
                        break;
                    case GridNavigationMethod.Next:
                        indexToSelect = currentIndex < grid.Records.Count - 1 ? currentIndex + 1 : currentIndex;
                        break;
                    case GridNavigationMethod.Last:
                        indexToSelect = grid.Records.Count - 1;
                        break;
                }                
            }
            if (grid.Records.Count > 0)
            {
                DataRecord record = grid.Records[indexToSelect] as DataRecord;
                if (record != null)
                {
                    SelectItem(grid, record.Data);
                }
            }
        }

..similarly, I have this code for selecting an item on the grid:
        public static void SelectItem(this RadGridView grid, object dataItem)
        {
            DataRecord record = grid.ItemsControl.FindRecord(dataItem) as DataRecord;    //grid.FindRecord(dataItem); ??
            if (record != null)
            {
                grid.SelectedItems.Clear();
                grid.SelectedItem = record.Data;
                grid.BringDataItemIntoView(grid.SelectedItem);
            }
        }



7 Answers, 1 is accepted

Sort by
0
Accepted
Milan
Telerik team
answered on 08 Sep 2009, 06:44 AM
Hi Inquisitor Jax,

We have introduced a new property called Items that gives you access to the data items of the grid. Coupled with the SelectedItems and SelectedItem properties you can do  everythingthat you could do with RadGridView.Records and DataRecord. Here is how your code can be modified:

public static void SelectItem(this RadGridView grid, GridNavigationMethod navigationMethod)  
        {  
            int indexToSelect = 0; //default to selecting the first item  
            if (grid.SelectedItem != null)  
            {  
                int currentIndex = grid.Items.IndexOf(grid.SelectedItem);  
                switch (navigationMethod)  
                {  
                    case GridNavigationMethod.First:  
                        indexToSelect = 0;  
                        break;  
                    case GridNavigationMethod.Previous:  
                        indexToSelect = currentIndex > 0 ? currentIndex - 1 : currentIndex;  
                        break;  
                    case GridNavigationMethod.Next:  
                        indexToSelect = currentIndex < grid.Items.Count - 1 ? currentIndex + 1 : currentIndex;  
                        break;  
                    case GridNavigationMethod.Last:  
                        indexToSelect = grid.Items.Count - 1;  
                        break;  
                }                  
            }  
            if (grid.Items.Count > 0)  
            {  
                object itemToSelect = grid.Items[indexToSelect];  
                if (itemToSelect != null)  
                {  
                    SelectItem(grid, itemToSelect);  
                }  
            }  
        }  
 
        public static void SelectItem(this RadGridView grid, object dataItem)  
        {  
            if (dataItem != null)  
            {  
                grid.SelectedItem = dataItem  
                grid.BringDataItemIntoView(grid.SelectedItem);  
            }  
        } 

Hope this helps.

Best wishes,
Milan
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
0
Inquisitor Jax
Top achievements
Rank 1
answered on 08 Sep 2009, 07:15 AM
Thanks for the reply Milan - works great.

1 thing: When I try and call BringDataItemIntoView(), a ArgumentOutOfRangeException occurs.
It seems it goes away if I call grid.UpdateLayout() before calling BringDataItemIntoView()

here's the stack trace:
  mscorlib.dll!System.ThrowHelper.ThrowArgumentOutOfRangeException(System.ExceptionArgument argument = index, System.ExceptionResource resource = ArgumentOutOfRange_Index) + 0x70 bytes
  mscorlib.dll!System.ThrowHelper.ThrowArgumentOutOfRangeException() + 0x21 bytes
  mscorlib.dll!System.Collections.Generic.List<Telerik.Windows.Controls.GridView.PhysicalToLogicalHeight>.this[int].get(int index = -1) + 0x2b bytes
  mscorlib.dll!System.Collections.ObjectModel.Collection<Telerik.Windows.Controls.GridView.PhysicalToLogicalHeight>.this[int].get(int index = -1) + 0x78 bytes
  Telerik.Windows.Controls.GridView!Telerik.Windows.Controls.GridView.GridViewVirtualizingPanel.BringRecordIntoView(Telerik.Windows.Data.Record record = {Telerik.Windows.Data.DataRecord}) + 0xa4 bytes
  Telerik.Windows.Controls.GridView!Telerik.Windows.Controls.GridView.GridViewItemsControl.BringDataItemIntoView(object data) + 0x5a bytes
  Telerik.Windows.Controls.GridView!Telerik.Windows.Controls.GridView.GridViewDataControl.BringDataItemIntoView(object dataItem) + 0x38 bytes

0
Milan
Telerik team
answered on 08 Sep 2009, 08:59 AM
Hello Inquisitor Jax,

Are you using the grid in a grouped scenario when you call BrindDataItemIntoView?

Greetings,
Milan
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
0
Inquisitor Jax
Top achievements
Rank 1
answered on 08 Sep 2009, 09:49 AM
no. The records are not grouped.

If I group the records, an ArgumentOutOfRangeException occurs, regardless of calling UpdateLayout.
I would have expected BringIntoView method to expand the group for me if it was collapsed.
0
Milan
Telerik team
answered on 09 Sep 2009, 04:09 PM
Hello Inquisitor Jax,

Well, the BringDataItemIntoView does not work well in grouped scenarios and we are working on improving that but you should not have such problems in flat mode. Unfortunately I was unable to reproduce the exception that you have mentioned.

If you are not happy to use UpdateLayout may I ask you to send us a sample application that will help us reproduce the error?

Regards,
Milan
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
0
Inquisitor Jax
Top achievements
Rank 1
answered on 10 Sep 2009, 07:04 AM
Milan,

The error occurs if there is another UI element over the grid.
My scenario is that I have a Grid with a Canvas.ZIndex of 100000 on the user control that I toggle the visibility of when I'm doing
operations so that the user cannot interact with the UI.

So when fetching records, creating columns dynamically and attached records to the view, the Grid locks the user control. 
After the records are attached, I select the first item (while the view is still locked), and this is when the Exception occurs.

Let me know if you need a sample project.

PS: I'm happy to use UpdateLayout in the meantime, but please let me know when / if the grouping gets fixed - it's a core part of our requirements for UI.

0
Inquisitor Jax
Top achievements
Rank 1
answered on 10 Sep 2009, 07:07 AM
PS: I'm also getting row visibility issues when selecting a record in this manner - I'll see if I can upload a sample project this evening (don't have access to my skydrive from work)
Tags
GridView
Asked by
Inquisitor Jax
Top achievements
Rank 1
Answers by
Milan
Telerik team
Inquisitor Jax
Top achievements
Rank 1
Share this question
or