DataGrid selected item highlight

I have stored ID of active row in my config . When I set the selected item in Task OnInitializedAsync() I want to highlight the row of Datagrid when the page started.

<RadzenDataGridSk AllowFiltering="true" AllowColumnResize="true" AllowAlternatingRows="true" @refref="grid"
FilterMode="FilterMode.Advanced" AllowSorting="true" PageSize="15" AllowPaging="true" PagerHorizontalAlign="HorizontalAlign.Left" ShowPagingSummary="true"
Data="orders" TItem="Zakazka_mdl" ColumnWidth="300px" LogicalFilterOperator="LogicalFilterOperator.Or" Density="Density.Compact" GridLines="DataGridGridLines.Vertical" SelectionMode="DataGridSelectionMode.Singl@bind-Value" @bind-Value="@selectedOrders">

I tried to use OnAfterRenderAsync(first render), but this method starts to run immediatly after calling first api data loading, when the Datagrid is still null. I don't know how to do it.
When I set the selected item by button, it works. I can see it because datasets in the Datagrid are dynamically filtered through the master dropdown.

The grid highlights the rows whose items are in the Value collection. Two things decide whether that works on startup:

  1. How items are matched. Without KeyProperty the grid compares the selected item with the rows by reference (or by your class's Equals override). If you create the selected item from the stored ID, e.g. new Zakazka_mdl { ID = savedId }, it is a different object than the one in orders and no row matches. Either take the instance from the loaded data:

    orders = await LoadOrders();
    selectedOrders = new List<Zakazka_mdl> { orders.FirstOrDefault(o => o.ID == savedId) };

    or set KeyProperty="ID" on the grid so rows are matched by ID.

  2. How the grid notices the change. It compares the Value list by reference, so assign a new list as above instead of adding to the list that is already bound (selectedOrders.Add(...) will not refresh the highlight).

Doing this in OnInitializedAsync after the data has loaded is enough; you do not need OnAfterRenderAsync or the grid reference. One more thing to check: with AllowPaging and PageSize 15 the row is highlighted only if it is on the current page. If the selected order can be on another page, call grid.GoToPage(index / PageSize) after the first render.

While checking this we found that KeyProperty was applied only when it appeared before Data in the markup. That is fixed for the next release; until then put KeyProperty before Data if you use it.

Thanks for your answer. I tried to do it what you wrote, but it didn't work. For explanation or some analysis here is som piece of used code. I suppose that Id does not have to be a part of Column collection, does it?

 <RadzenDataGridSk AllowFiltering="true" AllowColumnResize="true" AllowAlternatingRows="true" @ref="grid"
                   FilterMode="FilterMode.Advanced" AllowSorting="true" 
                   KeyProperty="Id"
                   Data="orders" TItem="Zakazka_mdl" ColumnWidth="300px" LogicalFilterOperator="LogicalFilterOperator.Or" Density="Density.Compact" GridLines="DataGridGridLines.Vertical" SelectionMode="DataGridSelectionMode.Single" @bind-Value="@selectedOrders" RowRender="@RowRender">

   <Columns>
     <RadzenDataGridColumn TItem="Zakazka_mdl" Property="Order_dod_code" Title="Číslo" Width="40px" TextAlign="TextAlign.Center" HeaderCssClass="rz-background-color-base-300" />
     <RadzenDataGridColumn TItem="Zakazka_mdl" Property="Katuz_name" Title="Kat. územie" Width="80px" HeaderCssClass="rz-background-color-base-200" />
     <RadzenDataGridColumn TItem="Zakazka_mdl" Property="Zakaznik_view" Title="Zákazník" Width="80px" HeaderCssClass="rz-background-color-base-300" />
     <RadzenDataGridColumn TItem="Zakazka_mdl" Property="Meral_view" Title="Vyhotovil" Width="80px" HeaderCssClass="rz-background-color-base-200" />
     <RadzenDataGridColumn TItem="Zakazka_mdl" Property="Zpmz" Title="ZPMZ č." Width="40px" HeaderCssClass="rz-background-color-base-300" />
     <RadzenDataGridColumn TItem="Zakazka_mdl" Filterable="false" Sortable="false" Width="40px" HeaderCssClass="rz-background-color-base-200">
       <HeaderTemplate>
         <RadzenButton Click=@(() => OnClickZakAdd(firmaName)) Icon=@MaterialIcons.AddIcon ButtonStyle="ButtonStyle.Success" Size="ButtonSize.ExtraSmall" Text="Nová" Class="me-1" Disabled=@livechange />
       </HeaderTemplate>
       <Template Context="Zakazka_mdl">
         <RadzenButton Icon="edit" ButtonStyle="ButtonStyle.Light" Variant="Variant.Flat" Size="ButtonSize.ExtraSmall" Click=@(() => OnClickZakDetail(Zakazka_mdl, firmaName)) /> @*Text="@Zakazka_mdl.Id.ToString()"*@
         <RadzenButton ButtonStyle="ButtonStyle.Danger" Icon="delete" Variant="Variant.Flat" Shade="Shade.Lighter" Size="ButtonSize.ExtraSmall" Class="my-1 ms-1" Click=@(() => OnClickZakDelete(Zakazka_mdl)) />
       </Template>
     </RadzenDataGridColumn>
     @* <RadzenDataGridColumn TItem="Zakazka_mdl" Property="Dodavatel_id" Title="Dodavatel_id." Width="80px" HeaderCssClass="rz-background-color-base-300" />*@
   </Columns>
 </RadzenDataGridSk>

then

 protected override async Task OnInitializedAsync()
 {........
 orders = await gpApi.GetFromJsonAsync<List<Zakazka_mdl>>($"orders?usrid={usrId}") ?? new(); // parameter z comba
 ordersDb = orders;
......
if (settings.Act_dod_id > 0)
{
  dodId = (long)settings.Act_dod_id;//nastavi combo
  OnChange(dodId); // nastavi dodavatela
  disabContinue = false;
}
else if (dods.Any() == true)
{
  dodId = dods.First().Dod_id;
  OnChange(dodId);
}

and OnChange(object did) method

void OnChange(object did)//, string name)//zmena combo dodavatel
{
  if (dods != null)
    dod = dods.FirstOrDefault(d => d.Dod_id.Equals(did));
  if (dod != null)
  {
    firmaName = dod.Nazov;
    disabContinue = false;
  }
  if (ordersDb != null)
  {
    orders = ordersDb.Where(o => o.Dodavatel_id.Equals(did)).ToList();

    // If the active supplier matches the selected one, highlight the active order in this grid view
    if (settings != null && settings.Act_dod_id == dod?.Dod_id && settings.Act_order_id > 0)
    {
      var activeOrder = orders.FirstOrDefault(o => o.Id == settings.Act_order_id);
      if (activeOrder != null)
      {
        selectedOrders = new List<Zakazka_mdl> { activeOrder };
        StateContainer.Zakazka = activeOrder;
      }
      else
      {
        selectedOrders = new List<Zakazka_mdl>();
        StateContainer.Zakazka = null;
      }
    }
    else
    {
      // If it's a different supplier or no active settings match, clear selection cleanly with a new empty list
      selectedOrders = new List<Zakazka_mdl>();
      StateContainer.Zakazka = null;
    }

    if (grid != null)
      grid.Reload();
  }
  StateContainer.Dodavatel = dod;

}

Id does not need to be a column, and the code you posted works as written on the current source. Two things can still remove the highlight. If your RowRender handler sets args.Attributes["class"], that class replaced the grid's own row classes including the selection highlight; from the next release it is merged instead. And if RadzenDataGridSk forwards @bind-Value before Data, a change of both in the same render dropped the selection; that is fixed for the next release too. Until then, append to the class in RowRender only via style, or keep @bind-Value after Data.

My RowRender method I add when highlight didn't work. And now it is only thing that works (commented line didn't work). It uses "style" now.

 void RowRender(RowRenderEventArgs<Zakazka_mdl> args)
 {
   args.Attributes.Add("style", $"font-weight: {(args.Data.Id == settings.Act_order_id ? "bold" : "normal")};");
   //args.Attributes.Add("class", args.Data.Id == settings.Act_order_id ? "rz-color-primary" : "rz-color-danger");
 } 

My RadzenDataGridSk is only for localized messages, nothing more.
Only thing I think about that maybe could cause some different behavior is my css classes overrides which creates "ultra compact" datagrid because my user wants to see more data in one screen. I tried to remove all not necessary vertical gaps. It looks like

/* ===================================== */
/* RADZEN CUSTOM OVERRIDES (MAUI BLAZOR) */
/* ===================================== */


/* --- Misc tweaks --- */

/* Expand/collapse column width */
.rz-datatable .rz-col-icon {
    --rz-grid-column-icon-width: 0.75rem;
}

/* Dropdown popup auto width */
.rz-dropdown-panel.rz-popup {
    width: auto !important;
}


/* ===================================== */
/* ULTRA COMPACT DATAGRID */
/* ===================================== */

/* 1. CELL BASE (remove vertical padding) */
/*.rz-datatable .rz-datatable-tbody > tr > td {
    padding-top: 0 !important;
    padding-bottom: 0 !important;
}*/
.rz-datatable .rz-datatable-tbody > tr > td {
  height: 1.5rem !important; /* Set a minimum consistent row height */
}

/* 2. CELL CONTENT WRAPPER (CRITICAL FIX) */
.rz-datatable .rz-cell-data {
    padding: 0 !important;
    margin: 0 !important;
    align-items: stretch !important; /* remove vertical gaps */
}


/* 3. FORCE CONTROLS TO FILL CELL */
.rz-datatable .rz-cell-data > * {
    height: 100%;
    margin: 0 !important;
}

/* 4. COMMON INPUT WRAPPERS */
.rz-datatable.rz-density-compact .rz-textbox,
.rz-datatable.rz-density-compact .rz-numeric,
.rz-datatable.rz-density-compact .rz-dropdown,
.rz-datatable.rz-density-compact .rz-inputtext,
.rz-datatable.rz-density-compact .rz-textarea,
.rz-datatable.rz-density-compact .rz-multiselect,
.rz-datatable.rz-density-compact .rz-calendar .rz-inputtext,
.rz-datatable.rz-density-compact .rz-spinner,
.rz-datatable.rz-density-compact .rz-colorpicker {
    margin: 0 !important;
    padding: 0 !important;
    height: 100%;
    box-sizing: border-box;
}


/* 5. REAL INPUT ELEMENT */
.rz-datatable.rz-density-compact input {
    height: 100%;
    margin: 0 !important;
    padding: 0 2px !important;
    line-height: 1rem;
    box-sizing: border-box;
}


/* 6. DROPDOWN */
.rz-datatable.rz-density-compact .rz-dropdown {
    height: 100%;
}

.rz-datatable.rz-density-compact .rz-dropdown-label {
    line-height: 1rem;
    padding: 0 2px;
}


/* 7. NUMERIC */
.rz-datatable.rz-density-compact .rz-numeric input {
    padding: 0 2px !important;
}


/* 8. TEXTBOX */
.rz-datatable.rz-density-compact .rz-textbox {
    padding: 0 2px !important;
}


/* 9. INPUT GROUP LABEL */
.rz-datatable.rz-density-compact .input-group-text {
    padding-left: 8px;
    padding-right: 8px;
    font-size: 0.875rem;
    height: 100%;
    display: flex;
    align-items: center;
}


/* 10. BUTTONS (prevent row stretching) */
.rz-datatable .rz-button {
    height: 100%;
    padding: 0 4px;
}


/* ===================================== */
/* DROPDOWN POPUP */
/* ===================================== */

.rz-dropdown-panel .rz-dropdown-item {
    padding-top: 2px !important;
    padding-bottom: 2px !important;
    line-height: 1.2rem;
}

.rz-dropdown-panel .rz-dropdown-items {
    padding-top: 0;
    padding-bottom: 0;
}

/* Custom small checkbox styling */
.rz-chkbox-sm .rz-chkbox-box {
  width: 16px !important;
  height: 16px !important;
}

.rz-chkbox-sm .rz-chkbox-icon {
  font-size: 12px !important;
  line-height: 14px !important;
}

/*Headers form Datagrid*/
/* Universal Header Colors - Solid & Adaptive for chnging theme*/

.custom-ds-header {
  /* Layer 1: Universal green tint | Layer 2: Theme's solid background color */
  background: linear-gradient(rgba(0, 200, 80, 0.15), rgba(0, 200, 80, 0.15)), var(--rz-grid-header-background-color, #ffffff) !important;
  color: #00c850 !important; /* A vibrant green that works on light and dark */
}

.custom-ns-header {
  /* Layer 1: Universal blue tint | Layer 2: Theme's solid background color */
  background: linear-gradient(rgba(0, 180, 216, 0.15), rgba(0, 180, 216, 0.15)), var(--rz-grid-header-background-color, #ffffff) !important;
  color: #00b4d8 !important; /* A clean cyan/blue */
}

.custom-zm-header {
  /* Layer 1: Universal red tint | Layer 2: Theme's solid background color */
  background: linear-gradient(rgba(239, 71, 111, 0.15), rgba(239, 71, 111, 0.15)), var(--rz-grid-header-background-color, #ffffff) !important;
  color: #ef476f !important; /* A punchy red/coral */
}

/* Force Radzen to respect the sticky z-index stack layout */
.rz-grid-table thead th {
  z-index: 2 !important;
}

.red-bold-numeric .rz-inputtext {
  color: red !important;
  font-weight: bold !important;
}

/* Divider between menu items */
hr.menu-divider {
  border: none;
  border-top: 1px solid var(--rz-border-color, #ccc);
  margin: 0.5rem 0;
}

We reproduced your exact startup flow (a RadzenDataGrid subclass that sets the text properties, KeyProperty before Data, @bind-Value after Data, OnInitializedAsync awaiting the API and then calling OnChange which filters the data, assigns a new list with the instance from the filtered data and calls grid.Reload(), plus your style-only RowRender) with a bUnit test. The row is highlighted on both 12.1.7 and 12.1.8.

Your CSS is not the cause. The highlight is a background color on the cells of the row with rz-state-highlight, and your overrides only change heights, padding and margins of the body cells and the header colors. The same stylesheet is active when selecting from a button, which you said works.

Two things to check on your side:

  1. Is RadzenDataGridSk a class that inherits RadzenDataGrid, or a .razor component that renders a RadzenDataGrid and forwards the parameters? If it is the latter and it forwards Value before Data, that is the bug fixed in 12.1.8 - update and it will work.

  2. Whether selectedOrders is actually non-empty after OnChange at startup. Your RowRender reads settings.Act_order_id directly, so the bold row proves the ID matches a row, not that the selection was set. If settings.Act_dod_id == dod?.Dod_id is false at startup (for example dod is not found because of a type mismatch between Dod_id and the long you pass), the else branch assigns an empty list and nothing is highlighted while the row still becomes bold. Log selectedOrders.Count right after OnChange to confirm.

The commented class line cannot change the text color on any version, because the cells set their own color which overrides a color class on the row.

Thank you for your time. I updated RadzenBlazor to 12.1.8, but strange things happen :slightly_smiling_face: and it still doesn't work in my app in spite of the activeorder is regular order. The row is not highlighted neither grid==null nor not null after update. Rowrender with bold font works and I will keep this now.

As you can see the dropdown is highlited correctly.

You can use our playground to provide a runnable code example that demonstrates the issues using the latest version.