(disregard) Radzen DataGrid: Duplicate Button Behavior - Retaining Selection of Newly Created Row

  1. Description: The scenario requires a button within a Radzen DataGrid that duplicates the current row.
  2. Undesired Behavior: After duplication, the system automatically reselects the original row, whereas the desired behavior is for the newly created row to be selected.
  3. Button's Event Handler (OnButtonDuplicerenClick):
  • A button click handler duplicates the selected row and inserts it into the DataGrid.
  • It selects the newly created row and performs some additional operations.
WebOrderModelDetail? _ClickedRow;
async Task OnButtonDuplicerenClick()
{
    _ClickedRow = _UiSelectedRow;
    var newRow = new WebOrderModelDetail();
    newRow = _m.Map<WebOrderModelDetail>(_UiSelectedRow);
    newRow.BestD_ID = 0;
    int insertIndex = _UiDetails.IndexOf(_UiSelectedRow);
    if (insertIndex >= 0)
    {
        _UiDetails.Insert(insertIndex + 1, newRow);
    }
    else
    {
        _UiDetails.Add(newRow);
    }
    _UiGrid.SelectRow(newRow);
    await _j.InvokeVoidAsync("selectText", _UiNumericHoev1.Element);
    _UiGrid.Reload();
    int page = (insertIndex+1) / _UiGrid.PageSize;
    _logger.LogWarning($"{insertIndex + 1},{page},{_UiGrid.PageSize}");
    _UiGrid.GoToPage(page);
    _UiGrid.Reload();
    _UiSelectedRow = newRow;
}
  1. OnRowSelect Event Handler:
  • This event handler triggers when a row is selected in the DataGrid.
  • It checks if the newly selected row is the same as the previously clicked row.
  • If they match, it reselects the previous row and resets the _ClickedRow variable, effectively canceling the selection.
  • Otherwise, it continues with row selection logic and additional operations.
   async Task OnRowSelect(WebOrderModelDetail d)
   {
      _logger.LogWarning($"prev: {_previousD?.BestD_ID ?? -1},curr:{d.BestD_ID},sele:{_UiSelectedRow.BestD_ID}");
       if (_previousD == d) { return; }
       if (d == _ClickedRow)
       {
           _UiGrid.SelectRow(_previousD);
           _ClickedRow = null;
           return; 
       }
       await _UiGrid.EditRow(d);
       _IsRowSelected = true;
       _UiSelectedRow = d;
       _originalHoev1 = _UiSelectedRow.BestD_Hoev1;
       await _j.InvokeVoidAsync("selectText", _UiNumericHoev1.Element);
   }
  1. Concerns: The use of _ClickedRow to manage this behavior is considered less than ideal, and manually selecting a row within the OnRowSelect handler is a source of concern.

Check my reply in your other thread. All you need to do is to add desired item to selected to the variable bond to Value.

Many thanks for your reply.

Sorry about the other thread, there was a typo there.

The clause in the DataGrid Html reads: @bind-Value=@_UiSelected.

That being said, I remain a lpuzzled. Please find an attempt at re-formulation below:

Issue Description:

In my application, I have implemented a feature where an original row within a Radzen DataGrid contains a "duplicate" button. When this button is clicked, it triggers the insertion of a new row that serves as a clone of the original row. The desired behavior is to set the newly created row as the selected row in the grid.

Challenge 1:

However, I've observed that the system tends to reselect the original row after the duplication process, which is not the intended behavior. While I've found a workaround to address this issue, it would be more preferable if there were a way to prevent this automatic reselection of the original row.

Challenge 2:

Another related concern arises when a row is deleted within the DataGrid. Regardless of my attempts to programmatically select a different row after deletion, it appears that no row remains selected.

Resolution Request:

I'm seeking guidance or solutions to overcome these challenges and ensure that the selected row behavior aligns with the intended user experience.

Got you. I see where my error is.

Thank you so much.