Apply Query to Drop Down List

Hi,

I have a drop-down list within a grid. 



the datasource for the drop-down list is an array of strings

protected string[] ClaimValueEnumValues = Enum.GetNames(typeof(Claims.PermissionType));

but sometimes - depending on the row data - I want to query filter the drop-down data source list (ie so that the ReadOnly option is not present). 

I have declared the column as such

<GridColumn HeaderText="Permission"
                                                Field=@nameof(ClaimModel.Permission)
                                                EditType="EditType.DropDownEdit"
                                                Width="120" MinWidth="120" MaxWidth="120">
                                        <EditTemplate Context="ClaimValueDropDownContext">
                                            <SfDropDownList TValue="DTOLibrary.Authentication.Claims.PermissionType" TItem="string"
                                                            DataSource="@ClaimValueEnumValues"
                                                            Query="@GridQuery(ClaimValueDropDownContext as ClaimModel)"
                                                            @bind-Value="@((ClaimValueDropDownContext as ClaimModel).Permission)">
                                            </SfDropDownList>
                                        </EditTemplate>
                                    </GridColumn>

and I have implemented the query function

protected Query GridQuery(ClaimModel context)
{
List<WhereFilter> predicates = new List<WhereFilter>();

if (Claims.FreightApp.EditOnlyClaims.Contains(context.ClaimType))
{
// add a filter to exclude anything that has been deleted
predicates.Add(new WhereFilter()
{
Field = nameof(ClaimModel.Permission),
Operator = "notequal",
value = "ReadOnly"
});
}

return new Query().Where(predicates);
}

but every drop-down in the grid always displays the same data, even though I have verified (by stepping through the code) that my WhereFilter is being added to predicates.

Thanks,
Jeremy








3 Replies 1 reply marked as answer

BC Berly Christopher Syncfusion Team February 8, 2021 12:01 PM UTC

Hi Jeremy, 

Greetings from Syncfusion support. 
 
We have analysed your query and we understand that you want to modify the DropDownList data source based on the record. Instead of querying we suggest you to achieve your requirement using below solution. We have filtered the ENUM values before assigning it to DropDownList based on the value.   
  
Refer the below code example.   
  
<SfGrid AllowPaging="true" DataSource="@Orders" Toolbar="@(new List<string>() { "Add""Edit""Delete""Cancel""Update" })">  
    <GridEditSettings AllowEditing="true" AllowDeleting="true" AllowAdding="true" Mode="@EditMode.Normal"></GridEditSettings>  
    <GridColumns>  
        <GridColumn Field=@nameof(Order.OrderID) HeaderText="Order ID" IsPrimaryKey="true" TextAlign="@TextAlign.Center" Width="140"></GridColumn>  
        <GridColumn Field=@nameof(Order.CustomerID) HeaderText="Customer Name" Width="150"></GridColumn>  
        <GridColumn Field=@nameof(Order.Gender) HeaderText="Gender" Width="150">  
            <EditTemplate Context="ClaimValueDropDownContext">  
                @{  
                    GetData(ClaimValueDropDownContext as Order);  
                    <SfDropDownList TValue="Gender" TItem="string" DataSource="@DdlData" @bind-Value="@((ClaimValueDropDownContext as Order).Gender)">  
                    </SfDropDownList>  
                }  
            </EditTemplate>  
        </GridColumn>  
        <GridColumn Field=@nameof(Order.Freight) HeaderText="Freight" EditType="EditType.NumericEdit" Format="C2" Width="140" TextAlign="@TextAlign.Right"></GridColumn>  
        <GridColumn Field=@nameof(Order.OrderDate) HeaderText="Order Date" EditType="EditType.DatePickerEdit" Format="d" Type="ColumnType.Date" Width="160"></GridColumn>  
    </GridColumns>  
</SfGrid>  
   
   
@code{  
    public List<Order> Orders { getset; }  
    public string[] DdlData { getset; }  
    protected void GetData(Order context)  
    {  
        if (context.CustomerID == "ALFKI" || context.CustomerID == "ANTON")  
        {  
            var gen = new List<string>(Enum.GetNames(typeof(Gender)));  
            gen.Remove("PreferNot");  
            gen.Remove("Others");  
            DdlData = gen.ToArray();  
        }  
        else  
        {  
            DdlData = Enum.GetNames(typeof(Gender));  
        }  
    }  
  
  
For your convenience we have prepared a sample which can be downloaded from below  
  
  
Please get back to us if you have further queries.  

Regards, 
Berly B.C 
 


Marked as answer

JE Jeremy February 14, 2021 09:14 AM UTC

Cool, thanks for that.


PO Prince Oliver Syncfusion Team February 15, 2021 05:14 AM UTC

Hi Jeremy, 

Most welcome. We are glad to assist you. 

Regards. 
Prince  


Loader.
Up arrow icon