Selecting first item in DropDownList for grid add/insert

1 Answer 80 Views
DropDownList Grid
improwise
Top achievements
Rank 1
Iron
Iron
improwise asked on 05 Jan 2022, 02:26 PM | edited on 05 Jan 2022, 02:27 PM

We have a grid with editor templates for a few types that are tied to a dropdownlist that is tied to a datasource.  When user clicks new/add they get an empty form (we are using popup) but we would like the drowdowns to be preselected with the first values in the datasource. How do we do this?  Is there some SelectedIndex property or similar available?

 

We are currently doing something like this but it does not seem to nice even though it works. 

Also, what is the best way to differentiate between add and update in the EditorTemplate, ie how do we know what is what? 


                <EditorTemplate>
                    @{
                        EditingCorporateCustomer = context as CorporateCustomerDto;

                        if (EditingCorporateCustomer.CompanyTypeId == 0)
                        {
                            EditingCorporateCustomer.CompanyTypeId = CompanyTypes[0].Id;

                        }


                        <TelerikDropDownList Data="@CompanyTypes" @bind-Value="@EditingCorporateCustomer.CompanyTypeId"
                                             Width="95%"
                                             TextField="Name"
                                             ValueField="Id">
                        </TelerikDropDownList>
                    }
                </EditorTemplate>


1 Answer, 1 is accepted

Sort by
0
Marin Bratanov
Telerik team
answered on 08 Jan 2022, 10:04 AM

Hello,

You have three approaches you can take:

  • have a default value in the model
  • provide an instance through the OnModelInit event
  • use a custom command button instead of the built-in Add command button, and use the grid state to predefine the model you will be inserting with to have that default value. You can find an example of this here, see the "Initiate Editing or Inserting of an Item" section.

 

Regards,
Marin Bratanov
Progress Telerik

Learn about the important changes coming in UI for Blazor 3.0 in January 2022!
Tags
DropDownList Grid
Asked by
improwise
Top achievements
Rank 1
Iron
Iron
Answers by
Marin Bratanov
Telerik team
Share this question
or