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.
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:
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:
or set KeyProperty="ID" on the grid so rows are matched by ID.
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?
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.