Hi, I have a data grid where selection of cells works well until the grid is reloaded. I have code that changes the text of a cell after the current cell is clicked, then reloads the grid. The cell / grid object selected items shows it as selected but the format does not change.
The XAML settings for the grid:
AutoGenerateColumns="False"
UserGroupMode="Disabled"
CurrentCellChanged="workerGrid_CurrentCellChanged"
CurrentCell="{Binding Cell, Mode=TwoWay}"
SelectionUnit="Cell"
CurrentCellStyle="{StaticResource CurrentCellStyle}"
SelectionMode="Multiple"
SelectedItem="{Binding SelectedWorker,Mode=TwoWay}"
>
<telerik:RadDataGrid.AlternateRowBackgroundStyle>
<telerik:DataGridBorderStyle BackgroundColor="LightGray"
BorderThickness="1"
BorderColor="BlanchedAlmond"/>
</telerik:RadDataGrid.AlternateRowBackgroundStyle>
<telerik:RadDataGrid.SelectionStyle>
<telerik:DataGridBorderStyle BackgroundColor="SeaGreen"
BorderColor="Wheat"
BorderThickness="2"/>
</telerik:RadDataGrid.SelectionStyle>
Part of the code that changes the cell content and then passes it to the view model, if I comment this out, the selection works well.
List<workerData> workers = new List<workerData>();
foreach (var myItem in workerGrid.ItemsSource as ObservableCollection<workerData>)
if (myItem != null)
{
//code that changes the text value of one cell, then loads it into the list of classes
}
//then passes it to the view model, where it is loaded. this is the same as first load
this.BindingContext = new ViewModel(workers);
I expect that I have a setting wrong / error in the code because I can see the selected cell etc. in this:
var tempSel = this.workerGrid.SelectedItems;
Thank you
this.BindingContext = new ViewModel(workers);
Hi, after going through your example, I realized I did not fully explain the issue. (and maybe did not fully understand it myself...)
The issue is I cannot select the cells after the view model / data is reloaded without either clicking on a button or selecting a cell. In other words, whatever click or event that is used to change the text is unable to re-select the cell and I cannot seem to select the cell without an event.
- Is there a way to get the selection code working with the text update code in one function?
- Or a way to get the selection code working when called from an event function?
Thank youvoid dataGrid_CurrentCellChanged(System.Object sender, Telerik.Maui.Controls.Compatibility.DataGrid.CurrentCellChangedEventArgs e) { //works without issue when a cell is changed updateGreen(); } void Button_Clicked(System.Object sender, System.EventArgs e) { List<workerData> workers = new List<workerData>(); int count = 0; //then passes it to the view model, where it is loaded. this is the same as first load this.BindingContext = new ViewModel(); foreach (var myItem in dataGrid.ItemsSource as ObservableCollection<workerData>) { //did this so it changes every other one if (myItem != null && count % 2 == 0) { myItem.Name = "Ivan"; } count++; } //this never works when it is here, only as a direct result of an event / click handler var firstMarketingItem = ((ObservableCollection<workerData>)this.dataGrid.ItemsSource).Where(p => p.Name != "Ivan"); foreach (var item in firstMarketingItem) { this.dataGrid.SelectCell(new DataGridCellInfo(item, this.dataGrid.Columns[0])); } // updateGreen(); } private void Button_Clicked_1(object sender, EventArgs e) { updateGreen(); } private void updateGreen() { //works without issue when called from either a button click or current cell change if (MainThread.IsMainThread == true) { var firstMarketingItem = ((ObservableCollection<workerData>)this.dataGrid.ItemsSource).Where(p => p.Name != "Ivan"); foreach (var item in firstMarketingItem) { this.dataGrid.SelectCell(new DataGridCellInfo(item, this.dataGrid.Columns[0])); } } else { MainThread.BeginInvokeOnMainThread(updateGreen1); } } private void updateGreen1() { var firstMarketingItem = ((ObservableCollection<workerData>)this.dataGrid.ItemsSource).Where(p => p.Name == "Ivan"); foreach (var item in firstMarketingItem) { this.dataGrid.SelectCell(new DataGridCellInfo(item, this.dataGrid.Columns[0])); } }