Hello Akash,
As long as you're setting the DataGrid.SelectedItem to on of the items in the ItemsSource, yes. For example, assuming you set up the SelectedItem with two-way binding:
You can select an item programmatically using one of the items in the items source, this line selects the first item:
Demo
For example,if the view model had the following code:
public
class
BaseViewModel : INotifyPropertyChanged
{
private
ObservableCollection<DataValue> displayItems;
private
DataValue selectedDisplayItem;
public
ObservableCollection<DataValue> DisplayItems
{
get
=> displayItems ?? (displayItems = GetDisplayItems());
set
{
if
(displayItems == value)
return
;
displayItems = value;
OnPropertyChanged();
}
}
public
DataValue SelectedDisplayItem
{
get
=> selectedDisplayItem;
set
{
if
(selectedDisplayItem == value)
return
;
selectedDisplayItem = value;
OnPropertyChanged();
}
}
private
ObservableCollection<DataValue> GetDisplayItems()
{
var srcDb =
new
ObservableCollection<DataValue>()
{
new
DataValue { Id =
"Data"
, Value =
"Data"
, Caption =
"Data"
},
new
DataValue { Id =
"Select"
, Value =
"Select"
, Caption =
"Select"
},
new
DataValue { Id =
"OperatorNo"
, Value =
"OperatorNo"
, Caption =
"OperatorNo"
},
new
DataValue { Id =
"OperatorName"
, Value =
"OperatorName"
, Caption =
"OperatorName"
}
};
return
srcDb;
}
public
event
PropertyChangedEventHandler PropertyChanged;
protected
virtual
void
OnPropertyChanged([CallerMemberName]
string
propertyName =
null
)
{
PropertyChanged?.Invoke(
this
,
new
PropertyChangedEventArgs(propertyName));
}
}
You could do the following to set up the DataGrid:
public
partial
class
MainPage : ContentPage
{
private
readonly
BaseViewModel viewModel;
public
MainPage()
{
InitializeComponent();
viewModel =
new
BaseViewModel();
BindingContext = viewModel;
SetUpDataGrid();
}
private
void
SetUpDataGrid()
{
grid_Source.AutoGenerateColumns =
false
;
grid_Source.SelectionUnit = DataGridSelectionUnit.Row;
grid_Source.SelectionStyle =
new
DataGridBorderStyle
{
BackgroundColor = Color.Orange,
BorderColor = Color.Orange,
BorderThickness =
new
Thickness(2)
};
var properties =
typeof
(DataValue).GetProperties();
foreach
(PropertyInfo property
in
properties)
{
grid_Source.Columns.Add(
new
DataGridTemplateColumn
{
HeaderText = property.Name,
Name = property.Name,
CellContentTemplate = GetTemplate(property.Name,
"Button"
, property.Name),
CellDecorationStyle =
new
DataGridBorderStyle
{
BorderThickness =
new
Thickness(0),
BorderColor = Color.Orange
}
});
}
// Set the ItemsSource
grid_Source.SetBinding(RadDataGrid.ItemsSourceProperty,
new
Binding(
"DisplayItems"
));
// Set the SelectedItem using two-way binding
grid_Source.SetBinding(RadDataGrid.SelectedItemProperty,
new
Binding(
"SelectedDisplayItem"
, BindingMode.TwoWay));
// Now, you can programmatically set the selected item vbecause of the two-way binding
// For example, if you wanted to select the first item in the list
viewModel.SelectedDisplayItem = viewModel.DisplayItems.FirstOrDefault();
}
public
DataTemplate GetTemplate(
string
psPropertyName,
string
controlType,
string
columnCaption)
{
return
new
DataTemplate(() =>
{
// Using StackLayout instead of View
var stackLayout =
new
StackLayout();
var oLabel =
new
Label()
{
BackgroundColor = Color.Transparent,
TextColor = Color.Black,
VerticalOptions = LayoutOptions.FillAndExpand,
HorizontalOptions = LayoutOptions.FillAndExpand,
};
oLabel.SetBinding(Label.TextProperty, psPropertyName);
//oLabel.Text = "Lorem Ipsum";
stackLayout.Children.Add(oLabel);
return
stackLayout;
});
}
}
Then you'll end up with the following at runtime as soon as the page opens:
Regards,
Lance | Tech Support Engineer, Sr.
Progress Telerik
Do you want to have your say when we set our development plans?
Do you want to know when a feature you care about is added or when a bug fixed?
Explore the
Telerik Feedback Portal
and vote to affect the priority of the items