Default value inline edit for GridColumn of type SfDropDownList

Hello,

I have a SfGrid with two columns of type SfDropDownList.

I want to select default values for this lists when a user create a new row (inline).

This is my grid code:

<SfGrid @ref="Grid" DataSource="@AgendaCollection.ContactAgendaList" AllowRowDra - Pastebin.com

this is the contact agenda class:

public class ContactAgenda

{

        public int Id { get; set; }

        public string ContactId { get; set; } = string.Empty;

        public string AgendaDescription { get; set; } = string.Empty;

        public ContactAgendaType Type { get; set; } = new ContactAgendaType();

        public ContactAgendaCategory Category { get; set; } = new ContactAgendaCategory();

        public string Value { get; set; } = string.Empty;

        public int Order { get; set; }

}


public class ContactAgendaType

{

public int Id { get; set; } = 1;

public string Description { get; set; } = string.Empty;

}


public class ContactAgendaCategory

{

public int Id { get; set; } = 0;

public string Description { get; set; } = string.Empty;

}

When I create a new row nothing is selected:

1.png


If I edit one row though everything works fine:

2.png



3 Replies

VN Vignesh Natarajan Syncfusion Team September 23, 2021 05:58 AM UTC

Hi Lorenzo,  
 
Thanks for contacting Syncfusion support.  
 
Query: “I want to select default values for this lists when a user create a new row (inline). 
 
We have analyzed your query and we suggest you to achieve your requirement by defining the value in OnActionBegin event of Grid. OnActionBegin event will be triggered when record is added. In that event, we suggest you to define the value for complex column.  
 
Refer the below code example.  
 
<SfGrid DataSource="@Employees" AllowRowDragAndDrop="true" Toolbar="@(new List<string>() { "Add" })" Height="300"> 
    <GridEvents OnActionBegin="ActionBegin" TValue="EmployeeData"></GridEvents> 
    <GridEditSettings AllowAdding="true" AllowDeleting="true" AllowEditing="true" ShowDeleteConfirmDialog="true"> 
  
        @*<Validator> 
                @{ ValidatorTemplateContext txt = context as ValidatorTemplateContext; } 
                <FluentValidationValidator /> 
                <ValidationMessage For="@(() => (txt.Data as ContactAgenda).AgendaDescription)"></ValidationMessage> 
                <ValidationMessage For="@(() => (txt.Data as ContactAgenda).Value)"></ValidationMessage> 
                <ValidationMessage For="@(() => (txt.Data as ContactAgenda).Type)"></ValidationMessage> 
                <ValidationMessage For="@(() => (txt.Data as ContactAgenda).Category)"></ValidationMessage> 
            </Validator>*@ 
  
    </GridEditSettings> 
    <GridColumns> 
        <GridColumn Field=@nameof(EmployeeData.EmployeeID) HeaderText="EmployeeID" IsPrimaryKey="true" TextAlign="TextAlign.Right" Width="120"></GridColumn> 
        <GridColumn Field="Name.FirstName" HeaderText="First Name" Width="150"> 
            <EditTemplate> 
                <SfDropDownList TValue="EmployeeName" TItem="EmployeeName" Placeholder="Tipologia" @bind-Value="@((context as EmployeeData).Name)" DataSource="@Emp"> 
                    <DropDownListFieldSettings Value="FirstName" Text="FirstName"></DropDownListFieldSettings> 
                </SfDropDownList> 
            </EditTemplate> 
        </GridColumn> 
        <GridColumn Field="Name.LastName" HeaderText="Last Name" Width="130"> 
            <EditTemplate> 
                <SfDropDownList TValue="EmployeeName" TItem="EmployeeName" Placeholder="Categoria" @bind-Value="@((context as EmployeeData).Name)" DataSource="@Emp"> 
                    <DropDownListFieldSettings Value="LastName" Text="LastName"></DropDownListFieldSettings> 
                </SfDropDownList> 
            </EditTemplate> 
        </GridColumn> 
        <GridColumn Field=@nameof(EmployeeData.Title) HeaderText="Title" Format="C2" TextAlign="TextAlign.Right" Width="120"></GridColumn> 
    </GridColumns> 
</SfGrid> 
  
@code{ 
    public List<EmployeeData> Employees { getset; } 
  
    public List<EmployeeName> Emp { getset; } 
  
    public void ActionBegin(ActionEventArgs<EmployeeData> args) 
    { 
        if (args.RequestType == Syncfusion.Blazor.Grids.Action.Add) 
        { 
            args.Data.Name.FirstName = "Nancy" 
            args.Data.Name.LastName = "Fuller" 
        } 
    } 
 
 
Consider the above scenario. We have defined the Field property with complex column (Name.FirstName & Name.LastName).  In the OnActionBegin event when requesttype as Add, we have defined the default value to FirstName and LastName property of Data argument.  
 
Kindly refer the below sample for your reference   
 
 
Please get back to us if you have further queries. 
 
Regards, 
Vignesh Natarajan 
  



LO Lorenzo September 23, 2021 07:01 AM UTC

Thank you for your reply.


What if I want to select the first value ? Is there a selectedindex property to set ? 



VN Vignesh Natarajan Syncfusion Team September 23, 2021 09:57 AM UTC

Hi Lorenzo, 
 
Thanks for the update.  
 
Query: “What if I want to select the first value ? Is there a selectedindex property to set ?  
 
DropDownList component has a Index property to select the value based on the index of record. But since Bind-Value property (required to save the selected record properly in Grid data) return the empty value, Index property value is overridden by empty value and empty string is displayed in DropDownList. We suggest you to define the first record value in OnActionBegin event to achieven your requirement.  
 
Refer the below code example.   
 
<SfGrid DataSource="@Employees" AllowRowDragAndDrop="true" Toolbar="@(new List<string>() { "Add" })" Height="300"> 
    <GridEvents OnActionBegin="ActionBegin" TValue="EmployeeData"></GridEvents> 
    <GridEditSettings AllowAdding="true" AllowDeleting="true" AllowEditing="true" ShowDeleteConfirmDialog="true"> 
  
        @*<Validator> 
                @{ ValidatorTemplateContext txt = context as ValidatorTemplateContext; } 
                <FluentValidationValidator /> 
                <ValidationMessage For="@(() => (txt.Data as ContactAgenda).AgendaDescription)"></ValidationMessage> 
                <ValidationMessage For="@(() => (txt.Data as ContactAgenda).Value)"></ValidationMessage> 
                <ValidationMessage For="@(() => (txt.Data as ContactAgenda).Type)"></ValidationMessage> 
                <ValidationMessage For="@(() => (txt.Data as ContactAgenda).Category)"></ValidationMessage> 
            </Validator>*@ 
  
    </GridEditSettings> 
    <GridColumns> 
        <GridColumn Field=@nameof(EmployeeData.EmployeeID) HeaderText="EmployeeID" IsPrimaryKey="true" TextAlign="TextAlign.Right" Width="120"></GridColumn> 
        <GridColumn Field="Name.FirstName" HeaderText="First Name" Width="150"> 
            <EditTemplate> 
                <SfDropDownList TValue="EmployeeName" TItem="EmployeeName" Placeholder="Tipologia" @bind-Value="@((context as EmployeeData).Name)" DataSource="@Emp"> 
                    <DropDownListFieldSettings Value="FirstName" Text="FirstName"></DropDownListFieldSettings> 
                </SfDropDownList> 
            </EditTemplate> 
        </GridColumn> 
        <GridColumn Field="Name.LastName" HeaderText="Last Name" Width="130"> 
            <EditTemplate> 
                <SfDropDownList TValue="EmployeeName" TItem="EmployeeName" Placeholder="Categoria" @bind-Value="@((context as EmployeeData).Name)" DataSource="@Emp"> 
                    <DropDownListFieldSettings Value="LastName" Text="LastName"></DropDownListFieldSettings> 
                </SfDropDownList> 
            </EditTemplate> 
        </GridColumn> 
        <GridColumn Field=@nameof(EmployeeData.Title) HeaderText="Title" Format="C2" TextAlign="TextAlign.Right" Width="120"></GridColumn> 
    </GridColumns> 
</SfGrid> 
  
@code{ 
    public List<EmployeeData> Employees { getset; } 
  
    public List<EmployeeName> Emp { getset; } 
  
    public void ActionBegin(ActionEventArgs<EmployeeData> args) 
    { 
        if (args.RequestType == Syncfusion.Blazor.Grids.Action.Add) 
        { 
            args.Data.Name = Emp[0]; 
        } 
    } 
  
    protected override void OnInitialized() 
    { 
        Emp = Enumerable.Range(1, 9).Select(x => new EmployeeName() 
        { 
            FirstName = (new string[] { "Nancy""Andrew""Janet""Margaret""Steven" })[new Random().Next(5)], 
            LastName = (new string[] { "Davolio""Fuller""Leverling""Peacock""Buchanan" })[new Random().Next(5)] 
        }).ToList(); 
  
. . . .  
  
    } 
 
 
Please get back to us if you have further queries.   
 
Regards, 
Vignesh Natarajan 


Loader.
Up arrow icon