[英]Retrieving drop down list items from list of controls
I am attempting to retrieve a ListItemCollection
from a List
of controls. 我正在尝试从控件List
中检索ListItemCollection
。 This List
contains many controls of different types - DropDownList
, Label
, TextBox
. 该List
包含许多不同类型的控件DropDownList
, Label
和TextBox
。
I would like to retrieve all ListItem
from all the DropDownList
controls contained in the original List
of controls. 我想从原始控件List
中包含的所有DropDownList
控件中检索所有ListItem
。
My thought process so far has been to extract all of the DropDownList
controls into a new list, and iterate though this to pull out each ListItem
- however, every DropDownList
control is coming up with 0 items 到目前为止,我的思考过程一直是将所有DropDownList
控件提取到一个新列表中,并进行迭代以拉出每个ListItem
但是,每个DropDownList
控件都有0个项目
ControlCollection cList = pnlContent.Controls;
List<DropDownList> ddlList = new List<DropDownList>();
foreach (Control c in cList)
{
if (c.GetType() == new DropDownList().GetType())
{
ddlList.Add((DropDownList)c);
}
}
ListItemCollection itemCollection = new ListItemCollection();
foreach (DropDownList ddl in ddlList)
{
foreach(ListItem li in ddl.Items)
{
itemCollection.Add(li);
}
}
I'm sure this is the wrong (and massively inefficient) way of doing this. 我确信这是错误的(而且效率很低)。 Any help would be appreciated. 任何帮助,将不胜感激。
This will do: 这样可以:
public IEnumerable<ListItem> GetListItems(ControlCollection controls)
{
return controls.OfType<DropDownList>().SelectMany(c => c.Items.OfType<ListItem>());
}
I currently do not have an installation where I can test this, but as a line of thought I would use Linq to do this. 我目前没有可以测试此功能的安装程序,但从思路上讲,我将使用Linq进行此操作。
Here is an example that you should be able to use; 这是一个您应该可以使用的示例;
var type = new DropDownList().GetType();
var listOfControl = from c in pnlContent.Controls
where c.GetType() == type
select ((DropDownList)c).Items;
Try this: 尝试这个:
var ddlList = cList.OfType<DropDownList>();
ListItemCollection itemCollection = new ListItemCollection();
// option 1
var temp = ddlList.Select(ddl => ddl.Items.Cast<ListItem>()).SelectMany(li => li).ToArray();
itemCollection.AddRange(temp);
// or option 2
var temp = ddlList.Select(ddl => ddl.Items.Cast<ListItem>()).SelectMany(li => li);
foreach (var listItem in temp)
{
itemCollection.Add(listItem);
}
comboBox1.Items.Add(button1);
comboBox1.Items.Add(button2);
comboBox1.Items.Add(dateTimePicker1);
comboBox1.Items.Add(checkBox1);
comboBox2.Items.Add(button3);
comboBox2.Items.Add(button4);
comboBox2.Items.Add(dateTimePicker2);
comboBox2.Items.Add(checkBox2);
comboBox3.Items.Add(button5);
comboBox3.Items.Add(dateTimePicker3);
comboBox3.Items.Add(checkBox3);
comboBox3.Items.Add(checkBox4);
List<ComboBox> ddlList = new List<ComboBox>();
foreach (Control c in panel1.Controls)
{
if (c is ComboBox)
{
ddlList.Add((ComboBox)c);
}
}
List<Control> itemCollection = new List<Control>();
foreach (ComboBox ddl in ddlList)
{
foreach (var li in ddl.Items)
{
itemCollection.Add((Control)li);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.