Grid event when cell value is changed to populate datasource for another column dropdownlist

I have a column 'Type' that is populated as a ForeignKey list, the value of which I need to use to update the datasource of the next column 'Metric' dropdownlist, see below.




I'm using normal edit mode and need an event to fire on the end of editing that cell or being editing of Metric cell, I don't see any events in the API for this scenario, how can I achieve this?



9 Replies

RN Rahul Narayanasamy Syncfusion Team April 20, 2020 11:17 AM UTC

Hi Mark, 

Greetings from Syncfusion. 

Query: Grid event when cell value is changed to populate datasource for another column dropdownlist 

We have validated your query and you want to populate the datasource for one dropdown column based on the selection of the another dropdown column. For this, we suggest you to use cascading dropdown list to achieve your requirement. Find the below code snippets, sample and documentation for your reference. 

 
<SfGrid AllowPaging="true" DataSource="@GridData" ShowColumnChooser="true" Toolbar="@(new List<string>() { "Add","Edit","Delete","Update","Cancel" })"> 
    <GridEvents OnActionBegin="OnActionBegin" TValue="OrdersDetails"></GridEvents> 
    <GridEditSettings AllowEditing="true" AllowDeleting="true" AllowAdding="true" Mode="@EditMode.Normal"></GridEditSettings> 
    <GridColumns> 
        . . . 
       <GridColumn Field=@nameof(OrdersDetails.ShipCountry) HeaderText="Ship Country" EditType="EditType.DropDownEdit" Width="150"> 
            <EditTemplate> 
                <SfDropDownList ID="ShipCountry" Placeholder="Select a Country" TItem="string" TValue="string" DataSource="@Countries"> 
                    <DropDownListEvents TValue="string" ValueChange="ValueChange"></DropDownListEvents> 
                    <DropDownListFieldSettings Text="ShipCountry" Value="ShipCountry"></DropDownListFieldSettings> 
                </SfDropDownList> 
            </EditTemplate> 
        </GridColumn> 
        <GridColumn Field=@nameof(OrdersDetails.ShipState) HeaderText=" ShipState" EditType="EditType.DropDownEdit" Width="150"> 
            <EditTemplate> 
                <SfDropDownList ID="ShipState" Placeholder="Select a State" TItem="string" Enabled="@Enabled" TValue="string" DataSource="@States"> 
                    <DropDownListFieldSettings Text="ShipState" Value="ShipState"></DropDownListFieldSettings> 
                </SfDropDownList> 
            </EditTemplate> 
        </GridColumn> 
    </GridColumns> 
</SfGrid> 
 
@code{ 
    public List<OrdersDetails> GridData { get; set; } 
    public List<string> Countries = new List<string>() { "United States", "Australia" }; 
    public List<string> States = new List<string>() { "New York", "Virginia", "Washington" }; 
    public bool Enabled = false; 
    . . . 
   public void ValueChange(@Syncfusion.Blazor.DropDowns.ChangeEventArgs<string> 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" }; 
        } 
        Enabled = true; 
    } 
    public void OnActionBegin(ActionEventArgs<OrdersDetails> args) 
    { 
        Enabled = false; 
    } 
    . . . 
} 


Reference

Please get back to us if you need further assistance. 

Regards, 
Rahul 



MB Mark Barton April 21, 2020 07:18 AM UTC

Hi Rahul

That sort of solves one problem, however I now see another issue when clicking on a row to edit, all the dropdowns lose their values. This happens also on the example you provide below, how can the values be retained?

Thanks
Mark


RN Rahul Narayanasamy Syncfusion Team April 22, 2020 12:25 PM UTC

Hi Mark, 

Thanks for the update. We are happy to hear that you have achieved the requirement. 

Query: however I now see another issue when clicking on a row to edit, all the dropdowns lose their values 

We have validated your query and you want to maintain the particular dropdown values while start editing. You can achieve this requirement by using Value property of DropDownList. Find the below code snippets and sample for your reference. 

<SfGrid AllowPaging="true" DataSource="@GridData" ShowColumnChooser="true" Toolbar="@(new List<string>() { "Add","Edit","Delete","Update","Cancel" })"> 
    <GridEvents OnActionBegin="OnActionBegin" TValue="OrdersDetails"></GridEvents> 
    <GridEditSettings AllowEditing="true" AllowDeleting="true" AllowAdding="true" Mode="@EditMode.Normal"></GridEditSettings> 
    <GridColumns> 
        . . . 
       <GridColumn Field=@nameof(OrdersDetails.ShipCountry) HeaderText="Ship Country" EditType="EditType.DropDownEdit" Width="150"> 
            <EditTemplate> 
                <SfDropDownList ID="ShipCountry" Value="@((context as OrdersDetails).ShipCountry)"  Placeholder="Select a Country" TItem="string" TValue="string" DataSource="@Countries"> 
                    <DropDownListEvents TValue="string" ValueChange="ValueChange"></DropDownListEvents> 
                    <DropDownListFieldSettings Text="ShipCountry" Value="ShipCountry"></DropDownListFieldSettings> 
                </SfDropDownList> 
            </EditTemplate> 
        </GridColumn> 
        <GridColumn Field=@nameof(OrdersDetails.ShipState) HeaderText=" ShipState" EditType="EditType.DropDownEdit" Width="150"> 
            <EditTemplate> 
                <SfDropDownList ID="ShipState" Value="@((context as OrdersDetails).ShipState)" TItem="string" Enabled="@Enabled" TValue="string" DataSource="@States"> 
                    <DropDownListFieldSettings Text="ShipState" Value="ShipState"></DropDownListFieldSettings> 
                </SfDropDownList> 
            </EditTemplate> 
        </GridColumn> 
    </GridColumns> 
</SfGrid> 


Note: In this above example, ShipCountry value is show while start editing. The value of the ShipState coumn is shown only if it matches the ShipCountry column(because the DataSource is based on ShipCountry column). 

Please get back to us if you need further assistance. 

Regards, 
Rahul 



DK Dallas Kostna October 19, 2020 04:34 PM UTC

I've created the following grid (in Blazor Server mode):
                <SfGrid @ref="LineItemGrid" DataSource="@zohoService.OMG_LineItems" Toolbar="@(new List<string>() { "Edit","Update","Cancel" })">
                    <GridEvents OnActionBegin="GridActionBegin" OnActionComplete="GridActionComplete" TValue="OMG_LineItem"></GridEvents>
                    <GridEditSettings AllowAdding="false" AllowEditing="true" AllowDeleting="false" Mode="EditMode.Normal"></GridEditSettings>
                    <GridColumns>
                        <GridColumn Field=@nameof(OMG_LineItem.ID) HeaderText="ID" AllowEditing="false" IsPrimaryKey="true" />
                        <GridColumn Field=@nameof(OMG_LineItem.Product_name) HeaderText="Logo" AllowEditing="false" />
                        <GridColumn Field=@nameof(OMG_LineItem.Manufacturer) HeaderText="Manufacturer" AllowEditing="false" />
                        <GridColumn Field=@nameof(OMG_LineItem.Style) HeaderText="Style" AllowEditing="false" />
                        <GridColumn Field=@nameof(OMG_LineItem.Color) HeaderText="Color" AllowEditing="false" />
                        <GridColumn Field=@nameof(OMG_LineItem.SKU) HeaderText="SKU_ID" AllowEditing="false" Visible="false"/>
                        <GridColumn Field=@nameof(OMG_LineItem.SKU_text) HeaderText="SKU" EditType="EditType.DropDownEdit">
                            <EditTemplate>
                                <SfDropDownList ID="dropdownSKU" @bind-Value="@((context as OMG_LineItem).SKU)" TItem="CreatorItem" TValue="string" DataSource="@zohoService.inventoryItems" SortOrder="Syncfusion.Blazor.Lists.SortOrder.Ascending" AllowFiltering="true">
                                    <DropDownListEvents TItem="CreatorItem" TValue="string" ValueChange="SKUChange"></DropDownListEvents>
                                    <DropDownListFieldSettings Value="ID" Text="SKU"></DropDownListFieldSettings>
                                </SfDropDownList>
                            </EditTemplate>
                        </GridColumn>
                        <GridColumn Field=@nameof(OMG_LineItem.SKU_color) HeaderText="SKU color_ID" AllowEditing="false" Visible="false" />
                        <GridColumn Field=@nameof(OMG_LineItem.SKU_color_text) HeaderText="SKU color" EditType="EditType.DropDownEdit">
                            <EditTemplate>
                                <SfDropDownList @ref="dropdownSKUColors" @bind-Value="@((context as OMG_LineItem).SKU_color)" TItem="CreatorItemColor" TValue="string" Query="@SKUColorQuery" DataSource="@zohoService.inventoryItemColors" SortOrder="Syncfusion.Blazor.Lists.SortOrder.Ascending" AllowFiltering="true">
                                    <DropDownListEvents TItem="CreatorItemColor" TValue="string" ValueChange="SKUColorChange"></DropDownListEvents>
                                    <DropDownListFieldSettings Value="ID" Text="SKU_for_color"></DropDownListFieldSettings>
                                </SfDropDownList>
                            </EditTemplate>
                        </GridColumn>
                        <GridColumn Field=@nameof(OMG_LineItem.Size_type) HeaderText="Size type" AllowEditing="false" />
                        <GridColumn Field=@nameof(OMG_LineItem.Qty) HeaderText="Qty" AllowEditing="false" />
                    </GridColumns>
                </SfGrid>

In the code, I have the following:
    private void SKUChange(Syncfusion.Blazor.DropDowns.ChangeEventArgs<string, CreatorItem> args)
    {
        lastSKU = args.ItemData.SKU;
        this.SKUColorQuery = new Query().Where(new WhereFilter() { Field = "ItemID", Operator = "equal", value = args.Value, IgnoreCase = false, IgnoreAccent = false });
    }

When I run this, if I change a SKU, the SKU Color column doesn't update right away. I need to exit edit mode and go back in - then the query works. What I am doing wrong here? Thanks!


RN Rahul Narayanasamy Syncfusion Team October 20, 2020 02:08 PM UTC

Hi Dallas, 

Greetings from Syncfusion. 

We have validated your query and we might suspect that you have faced the problem while updating the edited dropdown values(updated value is not updated properly in the Grid). While defining the components in EditTemplate, you need to define ID(Id value which is same as Field name of that column) property in the corresponding component. Find the below code snippets for your reference. 

 
<SfGrid AllowPaging="true" DataSource="@GridData" ShowColumnChooser="true" Toolbar="@(new List<string>() { "Add","Edit","Delete","Update","Cancel" })"> 
    <GridEvents OnActionBegin="OnActionBegin" TValue="Orders"></GridEvents> 
    <GridEditSettings AllowEditing="true" AllowDeleting="true" AllowAdding="true" Mode="@EditMode.Normal"></GridEditSettings> 
    <GridColumns> 
        . . . 
        <GridColumn Field=@nameof(Orders.ShipCountry) HeaderText="Ship Country" EditType="EditType.DropDownEdit" Width="150"> 
            <EditTemplate> 
                <SfDropDownList ID="ShipCountry" Placeholder="Select a Country" TItem="string" TValue="string" @bind-Value="@((context as Orders).ShipCountry)" DataSource="@Countries"> 
                    <DropDownListEvents TItem="string" TValue="string" ValueChange="ValueChange"></DropDownListEvents> 
                    <DropDownListFieldSettings Text="ShipCountry" Value="ShipCountry"></DropDownListFieldSettings> 
                </SfDropDownList> 
            </EditTemplate> 
        </GridColumn> 
        <GridColumn Field=@nameof(Orders.ShipState) HeaderText=" ShipState" EditType="EditType.DropDownEdit" Width="150"> 
            <EditTemplate> 
                <SfDropDownList ID="ShipState" Placeholder="Select a State" TItem="string" Enabled="@Enabled" TValue="string" @bind-Value="@((context as Orders).ShipState)" DataSource="@States"> 
                    <DropDownListFieldSettings Text="ShipState" Value="ShipState"></DropDownListFieldSettings> 
                </SfDropDownList> 
            </EditTemplate> 
        </GridColumn> 
    </GridColumns> 
</SfGrid> 

If it does not meet your requirement or if we misunderstood your requirement, then could you please share more information about your requirement. It will be helpful to validate and provide a better solution. 

Regards, 
Rahul 



DK Dallas Kostna October 21, 2020 07:24 PM UTC

Hmmm...that didn't seem to help.

As before, the data in the second dropdown loads once the first is selected - however, I need to change the first column, update and then edit again in order to see the correct set of values in the second column.

Curiously, I tried downloading the sample above and it works perfectly - UNTIL I update the components to the latest version (which my project uses)...then it stop working.




RN Rahul Narayanasamy Syncfusion Team October 22, 2020 04:45 PM UTC

Hi Dallas, 

Thanks for the update. 

We have validated your query with the provided information. We suggest you to add the below codes in your application to achieve this requirement in our latest versions. We have also prepared a sample by using the shared codes in our latest version 18.3.0.42.  
 
 
<SfGrid @ref="Grid" AllowPaging="true" DataSource="@GridData" ShowColumnChooser="true" Toolbar="@(new List<string>() { "Add","Edit","Delete","Update","Cancel" })"> 
    <GridEvents OnActionBegin="OnActionBegin" OnActionComplete="ActionCompletedHandler" TValue="Orders"></GridEvents> 
    <GridEditSettings AllowEditing="true" AllowDeleting="true" AllowAdding="true" Mode="@EditMode.Normal"></GridEditSettings> 
    <GridColumns> 
        . . . 
   </GridColumns> 
</SfGrid> 
 
@code{ 
    SfGrid<Orders> Grid; 
    public List<Orders> GridData { get; set; } = new List<Orders>(); 
    . . . 
   public bool Enabled { get; set; } 
    . . . 
   public void OnActionBegin(ActionEventArgs<Orders> args) 
    { 
        if (args.RequestType == Syncfusion.Blazor.Grids.Action.BeginEdit) 
        { 
            Enabled = false; 
            Grid.PreventRender(false); 
        } 
    } 
    public void ActionCompletedHandler(ActionEventArgs<Orders> args) 
    { 
        if (args.RequestType == Syncfusion.Blazor.Grids.Action.BeginEdit) 
        { 
            Grid.PreventRender(false); 
        } 
    } 
} 


Please let us know if you have any concerns. 

Regards, 
Rahul 



DK Dallas Kostna October 23, 2020 01:41 AM UTC

Awesome - works perfectly - thanks so much!


RN Rahul Narayanasamy Syncfusion Team October 23, 2020 07:33 AM UTC

Hi Dallas, 
 
Thanks for the update. 
 
We are happy to hear that the provided solution helpful to resolve the problem. 
 
Please get back to us if you need further assistance. 
 
Regards, 
Rahul 


Loader.
Up arrow icon