Question

I have one RadComboBox. I fill it with a function which returns for example {Stack, Over, Flow, StackOverFlow} When I click RadCombobox that items are listed.

And I want to give first place for empty element.

And I tried to do below:

var stack= GetItems(SiteId);
RadComboBox1.Items.Add(new RadComboBoxItem("", "0"));
RadComboBox1.DataSource = stack;  

RadComboBox1.DataTextField = "Name";
RadComboBox1.DataValueField = "Id";
RadComboBox1.DataBind();

RadComboBox1.AllowCustomText = false;

There is no change. Only {Stack, Over, Flow, StackOverFlow} are listed.

When I write below code

var stack= GetItems(SiteId);

RadComboBox1.DataSource = stack;  

RadComboBox1.DataTextField = "Name";
RadComboBox1.DataValueField = "Id";
RadComboBox1.DataBind();
RadComboBox1.Items.Add(new RadComboBoxItem("xyz", "0"));
RadComboBox1.AllowCustomText = false;

Only {Stack, Over, Flow, StackOverFlow, xyz} are listed.

But I dont get the result which I want.

And the design side is below.

<telerik:RadComboBox ID="RadComboBox1" runat="server" Width="120px" MarkFirstMatch="true" Filter="Contains"></telerik:RadComboBox>

How can I do?

I want to list { " " , "Stack", "Over", "Flow", "StackOverFlow"}

Was it helpful?

Solution

Using your first choice, add RadComboBox1.AppendDataBoundItems = true before you call DataBind(). This means it won't clear out the existing items before adding the items from the data bind. You will need to manually clear the items beforehand if necessary:

RadComboBox1.Items.Clear();
RadComboBox1.ClearSelection();

OTHER TIPS

When you set RadComboBox DataSource property it will clear all the items in the RadComboBox.Items then iterates through the IEnumerable object which is set to it and add them to the items collection. In your case you can add all of your items manually using radComboBox.Items.Add().

var stack= GetItems(SiteId);
//Add your empty item. 
RadComboBox1.Items.Add(new RadComboBoxItem("", "0")); 

//Add all the other items
foreach(var item in stack)
{
    RadComboBox1.Items.Add(new RadComboBoxItem(item.Name, item.Id))
}  

RadComboBox1.DataTextField = "Name"; 
RadComboBox1.DataValueField = "Id";

Or you can add your empty item to the collection first and then bind it to the RadComboBox(I assume stack is a List of StackItem)

List<StackItem> stack = GetItems(SiteId);
//Add your empty item. 
stack.Insert(0, new StackItem(){Name = "", Id = 0});

//Set the DataSource
RadComboBox1.DataSource = stack;

RadComboBox1.DataTextField = "Name"; 
RadComboBox1.DataValueField = "Id";

Although usually it's not a good idea to change your collection(latter example) for such cases.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top