This question is locked. New answers and comments are not allowed.
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);
}
}
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);
}
}