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:
If I edit one row though everything works fine:
|
<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 { get; set; }
public List<EmployeeName> Emp { get; set; }
public void ActionBegin(ActionEventArgs<EmployeeData> args)
{
if (args.RequestType == Syncfusion.Blazor.Grids.Action.Add)
{
args.Data.Name.FirstName = "Nancy";
args.Data.Name.LastName = "Fuller";
}
}
|
Thank you for your reply.
What if I want to select the first value ? Is there a selectedindex property to set ?
|
<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 { get; set; }
public List<EmployeeName> Emp { get; set; }
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();
. . . .
}
|