I have page with a DropDownList populated with
countries. Under the DropDownList I have
a DataGrid which displays cities linked to the country selected from the
DropDownList. The data is held in a
Microsoft SQL database, and I’m using Dapper for CRUD operations.
Initially I appeared to be getting random behaviour where
the ValueChange on the DropDownListEvents didn’t appear to be working. However, following a lot of investigation, under
certain, reproduceable, situations the DataGrid fails to display the correct
data. In general, if I select two
countries (A then B) each with a city associated with them, followed by a
country (C) with no associated city, followed by country D (which has an
associated city), then back to country B again,
then the DataGrid displays the city associated with country D. (Note: country D could be country A.)
Is there a problem with the DataGrid? The “Cities_GetByCountry” seems to be
Please see the linked YouTube video: https://youtu.be/kQGorcdi0tQ
The HTML and code for the page is shown below: (I am happy
to share any other information you require).
Using 18.3.0.48
@page "/countriesandcities"
@using BlazorCountries.Data
@inject ICountriesService CountriesService
@inject ICitiesService CitiesService
<h3>Countries and Cities</h3>
<div class="control_wrapper">
<SfDropDownList TItem="Countries"
TValue="int"
DataSource="@countries"
Placeholder="Select a
country"
PopupHeight="200px"
PopupWidth="250px">
<DropDownListFieldSettings Text="CountryName" Value="CountryId"></DropDownListFieldSettings>
<DropDownListEvents TItem="Countries" TValue="int" ValueChange="@OnChange"></DropDownListEvents>
</SfDropDownList>
</div>
<br />
<div>
<SfGrid @ref="CitiesGrid"
DataSource="@cities"
AllowSorting="true"
AllowResizing="true"
Height="200">
<GridColumns>
<GridColumn Field="@nameof(Cities.CityId)"
HeaderText="City ID"
TextAlign="@TextAlign.Left"
Width="20">
</GridColumn>
<GridColumn Field="@nameof(Cities.CountryId)"
HeaderText="Country ID"
TextAlign="@TextAlign.Left"
Width="20">
</GridColumn>
<GridColumn Field="@nameof(Cities.CityName)"
HeaderText="City Name"
TextAlign="@TextAlign.Left"
Width="50">
</GridColumn>
<GridColumn Field="@nameof(Cities.CityPopulation)"
HeaderText="Population"
Format="n"
TextAlign="@TextAlign.Right"
Width="50">
</GridColumn>
</GridColumns>
</SfGrid>
</div>
<style>
.control_wrapper {
width: 250px;
}
</style>
@code{
IEnumerable<Countries> countries;
IEnumerable<Cities> cities;
SfGrid<Cities> CitiesGrid;
[Parameter]
public int SelectedCountryId { get; set; } = 0;
protected override async Task OnInitializedAsync()
{
//Populate the
list of countries objects from the Countries table.
countries = await CountriesService.CountriesGetAll();
}
private async Task
OnChange(Syncfusion.Blazor.DropDowns.ChangeEventArgs<int, Countries> args)
{
SelectedCountryId =
args.ItemData.CountryId;
cities = await CitiesService.Cities_GetByCountry(this.SelectedCountryId);
CitiesGrid.Refresh();
this.StateHasChanged();
}
}
The code for the service that gets the data is:
public async
Task<IEnumerable<Cities>> Cities_GetByCountry(int @CountryId)
{
IEnumerable<Cities> cities;
var parameters = new DynamicParameters();
parameters.Add("CountryId", CountryId,
DbType.Int32);
using (var conn = new
SqlConnection(_configuration.Value))
{
cities = await conn.QueryAsync<Cities>("spCities_GetByCountry",
parameters, commandType: CommandType.StoredProcedure);
}
return cities;
}
I think this problem has not been solved yet... any news?
I have a SDropDownList in a Grid dialog and when a Query ( from other SdropDownList) is applied to it, the second SDropDownList is not refreshed
Hi Manuel,
Thanks for contacting Syncfusion support.
We understand that you want to achieve cascading DropDownList behavior in Grid edit mode. We have already documented this topic in our UG documentation which can be referred to below
https://blazor.syncfusion.com/documentation/datagrid/how-to/cascading-dropdownlist-with-grid-editing
Please get back to us if you have further queries.
Regards,
Vignesh Natarajan
Many thanks
Thanks for the update. Please get back to us if you have further queries.