How to load data in Second Combobox based on First Combobox Value

I have check Documentation of Blazor syncfusion Combox Data Loading from List.It works fine.I would like to know below points 

  • How to load data in Second Combobox based on First Combobox Value (in normal EditForm)
  • How to load data in Second Combobox based on First Combobox Value (in Grid using Batch Mode)

3 Replies

JM Jeyanth Muthu Pratheeban Sankara Subramanian Syncfusion Team April 5, 2021 09:25 AM UTC

Hello Ismail, 

Greetings from Syncfusion support.

Query 1:
 

How to load data in Second Combobox based on First Combobox Value (in normal EditForm) 

Response:
We checked your query of achieving the cascading functionalities of Combobox. We prepared a sample for cascading functionalities of the ComboBox and attached it below for your convenience. In the attached sample, if a country is chosen, then corresponding states for the chosen county is loaded in the next combobox. Kindly refer the below code and attached sample. Hope this helps to meet your requirement.  

 
<div class="col-lg-12 control-section">  
    <div class="control-wrapper">  
        <div class="padding-top">  
            <SfComboBox TValue="string" TItem="Countries" Placeholder="Select a country" AllowCustom="false" PopupHeight="auto" DataSource="@Country">  
                <ComboBoxEvents TValue="string" TItem="Countries" ValueChange="ChangeCountry"></ComboBoxEvents>  
                <ComboBoxFieldSettings Text="CountryName" Value="CountryId"></ComboBoxFieldSettings>  
            </SfComboBox>  
        </div>  
        <div class="padding-top">  
            <SfComboBox TValue="string" TItem="State" Enabled="@EnableStateDropDown" @bind-Value="@StateValue" Placeholder="Select a state" Query="@StateQuery" AllowCustom="false" PopupHeight="auto" DataSource="@States">  
                <ComboBoxEvents TValue="string" TItem="State" ValueChange="ChangeState"></ComboBoxEvents>  
                <ComboBoxFieldSettings Text="StateName" Value="StateId"></ComboBoxFieldSettings>  
            </SfComboBox>  
        </div>  
    </div>  
</div>  
  
@code {  
.  .  .  
public void ChangeCountry(Syncfusion.Blazor.DropDowns.ChangeEventArgs<string, Countries> args)  
    {  
        this.EnableStateDropDown = !string.IsNullOrEmpty(args.Value);  
        this.EnableCitytDropDown = false;  
        this.StateQuery = new Query().Where(new WhereFilter() { Field = "CountryId", Operator = "equal", value = args.Value, IgnoreCase = false, IgnoreAccent = false });  
        this.StateValue = null;  
    }  
    public void ChangeState(Syncfusion.Blazor.DropDowns.ChangeEventArgs<string, State> args)  
    {  
        this.EnableCitytDropDown = !string.IsNullOrEmpty(args.Value);  
    }  
.  .  .   
}  
  



Query 2:

 
How to load data in Second Combobox based on First Combobox Value (in Grid using Batch Mode) 


Response:

We also analyzed the reported query and we suggest you to achieve your requirement using the EditTemplate feature of the Grid. Using EditTemplate we can render the custom component in the Grid column while editing. Using the ComboxBox change event, we can update the second combo box datasource.  
  
Refer to the below code example.   
  
<SfGrid @ref="GridRef" AllowPaging="true" DataSource="@GridData" ShowColumnChooser="true" Toolbar="@(new List<string>() { "Add","Edit","Delete","Update","Cancel" })">     
    <GridEditSettings AllowEditing="true" AllowDeleting="true" AllowAdding="true" Mode="@EditMode.Batch"></GridEditSettings>  
    <GridColumns>  
        <GridColumn Field=@nameof(Orders.OrderID) HeaderText="Order ID" IsPrimaryKey="true" TextAlign="@TextAlign.Center" Width="140"></GridColumn>  
        <GridColumn Field=@nameof(Orders.Freight) HeaderText="Freight" EditType="EditType.NumericEdit" Format="C2" Width="140" TextAlign="@TextAlign.Right"></GridColumn>  
        <GridColumn Field=@nameof(Orders.OrderDate) HeaderText="Order Date" EditType="EditType.DatePickerEdit" Format="d" Type="ColumnType.Date" Width="160"></GridColumn>  
        <GridColumn Field=@nameof(Orders.ShipCountry) HeaderText="Ship Country" EditType="EditType.DropDownEdit" Width="150">  
            <EditTemplate>  
                <SfComboBox ID="ShipCountry" Placeholder="Select a Country" TItem="string" TValue="string" @bind-Value="@((context as Orders).ShipCountry)" DataSource="@Countries">  
                    <ComboBoxEvents TValue="string" TItem="string" ValueChange="ValueChange"></ComboBoxEvents>  
                    <ComboBoxFieldSettings Text="ShipCountry" Value="ShipCountry"></ComboBoxFieldSettings>  
                </SfComboBox>  
            </EditTemplate>  
        </GridColumn>  
        <GridColumn Field=@nameof(Orders.ShipState) HeaderText=" Ship State" EditType="EditType.DropDownEdit" Width="150">  
            <EditTemplate>  
                <SfComboBox ID="ShipState" Placeholder="Select a State" TItem="string" TValue="string" @bind-Value="@((context as Orders).ShipState)" DataSource="@States">  
                    <ComboBoxFieldSettings Text="ShipState" Value="ShipState"></ComboBoxFieldSettings>  
                </SfComboBox>  
            </EditTemplate>  
        </GridColumn>  
    </GridColumns>  
</SfGrid>  
  
public void ValueChange(@Syncfusion.Blazor.DropDowns.ChangeEventArgs<stringstring> args){    if (args.Value == "United States")    {        States = new List<string>() { "New York""Virginia""Washington" };    }    else if (args.Value == "Australia")    {        States = new List<string>() { "Queensland""Tasmania""Victoria" };    }              } 
  
   
For your convenience, we have prepared a sample which can be downloaded from below  
  
  
Refer to our UG documentation for your reference  
  
  
Please get back to us if you have further queries.  


Regards, 
Jeyanth. 



MS Michele Semprini April 16, 2023 03:32 PM UTC

Hi,

i'm trying to do something like that in a generic Component.

Here are my 2 combobox in 2 columns od a datagrid : When I edit a row of the grid and select an item in the first combobox, the ValueChange(...) is correctly fired but no changes in lookupChildren (DataSource of the second one) is shown in the second combo in the grid


Someone can tell me if there's something wrong in the definition of the GridForeignColumns ?



                       <GridForeignColumn Field="ID1" HeaderText="Header1"

                           EditType="EditType.DropDownEdit" TextAlign="TextAlign.Left"

                           ForeignKeyField="Id"

                           ForeignKeyValue="Description"

                              ForeignDataSource="@lookups">

                           <EditTemplate>

                               <SfComboBox ID="IdCdC1" Placeholder="Select a CdC "

                                       TItem="LookupItem<int>" TValue="int"

                                       DataSource="@lookups">

                                   <ComboBoxFieldSettings Text="Description" Value="Id"></ComboBoxFieldSettings>

                                   <ComboBoxEvents TValue="int" TItem="LookupItem<int>" ValueChange="ValueChange"></ComboBoxEvents>

                               </SfComboBox>


                           </EditTemplate>

                       </GridForeignColumn>



<GridForeignColumn Field="ID2" HeaderText="Header2"

      EditType="EditType.DropDownEdit" TextAlign="TextAlign.Left"

       ForeignKeyField="Id"

       ForeignKeyValue="Description"

       ForeignDataSource="@lookupChildren">

    <EditTemplate>

        <SfComboBox ID="IdCdC2" Placeholder="Select a CdC2"

            TItem="LookupItem<int>" TValue="int"

            DataSource="@lookupChildren">

            <ComboBoxFieldSettings Text="Description" Value="Id"></ComboBoxFieldSettings>

        </SfComboBox>

    </EditTemplate>

</GridForeignColumn>



        public List<LookupItem<int>> lookups;

        public List<LookupItem<int>> lookupChildren { get; set; } 


        public void ValueChange(@Syncfusion.Blazor.DropDowns.ChangeEventArgs<int, LookupItem<int>> args)

        {

            lookupChildren.Clear();

             lookupChildren.Add(new LookupItem<int>(1, "1"));

             lookupChildren.Add(new LookupItem<int>(2, "2"));

             //*** or load something else in children ***

        }




    public class LookupItem<T>

    {

        public T Id { get; set; }

        public string Description { get; set; }

    }


thnks a lot, m.




PS Prathap Senthil Syncfusion Team April 17, 2023 07:37 AM UTC

Hi Michele,

Based on the reported problem We would like to inform you that we have prevented unwanted rendering of the Grid component during the external action for better performance. So we suggest you use
PreventRender(false) on the value change event to reflect the changes in the DataGrid. Kindly refer to the attached code snippet for your reference.


<SfGrid @ref="Grid"……>

 

</SfGrid> 
@code{
   SfGrid<TValue> Grid { get; set; }
  public void ValueChange(@Syncfusion.Blazor.DropDowns.ChangeEventArgs<int, LookupItem<int>> args)

        {

            lookupChildren.Clear();

lookupChildren.Add(new LookupItem<int>(1, "1"));

lookupChildren.Add(new LookupItem<int>(2, "2"));
           Grid.PreventRender(false);

        }

}


If you still face difficulties, then kindly share us an simple issue reproduceable sample. It will be very helpful for us to validate the reported issue.


Regards,
Prathap S


Loader.
Up arrow icon