简体   繁体   English

如何列出RadComboBox为空的项目

[英]How to list items with an empty for RadComboBox

I have one RadComboBox. 我有一个RadComboBox。 I fill it with a function which returns for example {Stack, Over, Flow, StackOverFlow} When I click RadCombobox that items are listed. 我用返回例如{Stack,Over,Flow,StackOverFlow}的函数填充它。当我单击RadCombobox时,将列出项目。

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. 仅列出{Stack,Over,Flow,StackOverFlow}。

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. 仅列出{Stack,Over,Flow,StackOverFlow,xyz}。

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" } 我想列出{ " ""Stack""Over""Flow""StackOverFlow" }

Using your first choice, add RadComboBox1.AppendDataBoundItems = true before you call DataBind() . 使用第一选择,在调用DataBind()之前添加RadComboBox1.AppendDataBoundItems = true 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();

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. 设置RadComboBox DataSource属性时,它将清除RadComboBox.Items中的所有项目,然后遍历设置为它的IEnumerable对象,并将它们添加到items集合中。 In your case you can add all of your items manually using radComboBox.Items.Add(). 您可以使用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) 或者,您可以先将空项目添加到集合中,然后将其绑定到RadComboBox(我假设堆栈是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. 尽管通常在这种情况下更改您的收藏集不是一个好主意(后例)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM