how to insert element in dropdown list

Hello, 
I would like to know if possible,
how to insert new element into existing dropdown list at 1 st position by external button and if possible that inserted element gets selected at the same time???
P.S dropdown list Refresh method does it work??? it freezes dropdown elements.
thank you.

1 Reply

SP Sureshkumar P Syncfusion Team May 4, 2020 12:09 PM UTC

Hi Aleqsandre, 
 
Greetings from Syncfusion support.  
 
Yes, you can add the data into initial position (0th index) and select those added list value. Kindly refer the below code example.  
 
<SfDropDownList @ref="DDLObject" TItem="GameFields" TValue="string" PopupHeight="230px" Placeholder="Select a game" @bind-Value="@DropVal" DataSource="@Games"> 
    <DropDownListFieldSettings Text="Text" Value="ID"></DropDownListFieldSettings> 
</SfDropDownList> 
 
 
<button @onclick="@AddDataSource">Click to add the datasource</button> 
 
 
@code 
{ 
    SfDropDownList<string, GameFields> DDLObject; 
 
    public class GameFields 
    { 
        public string ID { get; set; } 
        public string Text { get; set; } 
    } 
    private ObservableCollection<GameFields> Games = new ObservableCollection<GameFields>() { 
        new GameFields(){ ID= "Game1", Text= "American Football" }, 
        new GameFields(){ ID= "Game2", Text= "Badminton" }, 
        new GameFields(){ ID= "Game3", Text= "Basketball" }, 
        new GameFields(){ ID= "Game4", Text= "Cricket" }, 
        new GameFields(){ ID= "Game5", Text= "Football" }, 
        new GameFields(){ ID= "Game6", Text= "Golf" }, 
        new GameFields(){ ID= "Game7", Text= "Hockey" }, 
        new GameFields(){ ID= "Game8", Text= "Rugby"}, 
        new GameFields(){ ID= "Game9", Text= "Snooker" } 
     }; 
    public string DropVal; 
 
    public void AddDataSource() 
    { 
        // add the datasource dynamically using addItems method  
        this.Games.Insert(0,new GameFields() { ID = "Game10", Text = "Tennis" });    
        this.DropVal = "Game10"; 
    }        
} 
 
 
We have created the sample based on your requirements. Please refer the sample here: https://www.syncfusion.com/downloads/support/directtrac/general/ze/DDLAddItem-1383598944  
 
Regards, 
Sureshkumar p 


Loader.
Up arrow icon