Datagrid cell will not select after grid reload

1 Answer 181 Views
DataGrid
superBren
Top achievements
Rank 1
superBren asked on 24 Oct 2022, 01:16 PM

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

superBren
Top achievements
Rank 1
commented on 27 Oct 2022, 06:11 PM

Hi, thank you, I think you solved the problem. In the function where I was changing the text, I was re-loading the data to a list / observable collection, then rebinding the grid with: 
this.BindingContext = new ViewModel(workers);
removing that, I was able to get your correct version to work with a quick example.  thanks very much
superBren
Top achievements
Rank 1
commented on 29 Oct 2022, 01:10 AM

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 you
  void 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]));
            }
    }

 

 

superBren
Top achievements
Rank 1
commented on 30 Oct 2022, 04:47 PM

Hi, switch to a style selector for the cell and I am all set, thank you for the help and effort

1 Answer, 1 is accepted

Sort by
0
Accepted
Antoan
Telerik team
answered on 27 Oct 2022, 03:38 PM

Hello Brendan,

I cannot seem to understand the issue completely. I made a reproduction app that reloads the grid on a button click however the selection seems to be working as expected. Could you give us more information about the scenario you desire to achieve. Also do you want to use keyboard navigation to change between the cells, a mouse click or both.

Keep in mind that the CurrentCell property and the SelectedItem property are different properties.

Information about the targeted platform will also be of use for us to investigate as it may appear only a specific platform.

In addition I have attached my test project, please refactor it when explaining the scenario to match your code as close as possible. I know this takes a lot of effort but it will help us do a throughout research. As this is a public forum thread, if you do not want to share your repro project to it, you can open a support ticket and attach it there.

I am looking forward to your reply.

Regards,
Antoan
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

Tags
DataGrid
Asked by
superBren
Top achievements
Rank 1
Answers by
Antoan
Telerik team
Share this question
or