Foreach一个列表框,拆分值并在Array中插入splitle value [英] Foreach a listbox, Split the value and Insert splitted value in Array
本文介绍了Foreach一个列表框,拆分值并在Array中插入splitle value的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有这个问题阻止我收集一组适当的数据。
我想要从列表框中取出物品并拆分物品。
分裂项目必须存储在一个数组中
我有这种语法。
I have this problem that prevents me to collect a proper set of data.
I want to Foreach the items from a listbox and split the items.
The splitted items must be store in a single Array
I have this syntax.
while (reader.Read())
{
var tmp = reader["Items"];
if (tmp != DBNull.Value)
{
ListBox1.Items.Add(tmp.ToString()); // The data that added in the Listbox is already
// 1 string ("ItemA,ItemB,ItemC,ItemD")
}
}
foreach (var items in ListBox1.Items)
{
myDb.Add(new MyItems() { string.Join(",", items) } );
//Here, I want to transfer the items from listbox.
}
我从这个过程得到的结果是1个数组中的单个数据,而不是多个数据。
实际结果:
The result I got from this process is a single data in 1 array, not multiple.
Actual Result:
{"ItemA,ItemB,ItemC,ItemD"}
预期结果:
Expected Result:
{ "ItemA", "ItemB", "ItemC", "ItemD" }
I found this usefull:
Here[^]
and I have this syntax now.
IEnumerable<string> row;
IEnumerable<string> permutations;
while (reader.Read())
{
var tmp = reader["Items"];
if (tmp != DBNull.Value)
{
ListBox1.Items.Add(tmp.ToString());
}
}
row = new string[] {ListBox1.Items.ToString()};
permutations = GetPermutations(row, delimiter: ",");
foreach (var permutation in permutations)
{
myDb.Add(new MyItems() {permutation});
}
Error: {System.Web.UI.WebControls.ListItemCollection}
推荐答案
试试这个
Try this
foreach (var items in ListBox1.Items)
{
myDb.Add(new MyItems() { items });
}
var array = myDB.ToArray();
也许试试:
Maybe try:
while (reader.Read()) {
// Here I would like to know how you assign you 'tmp' variable?
if (tmp != DBNull.Value) {
ListBox1.Items.Add(tmp.ToString()); // The data that added in the Listbox is already
// 1 string ("ItemA,ItemB,ItemC,ItemD")
}
}
List<string> list = new List<string>();
foreach (var items in ListBox1.Items) {
// myDb.Add(new MyItems() { string.Join(",", items) } );
//Here, I want to transfer the items from listbox.
list.AddRange(((string)items).Split(','));
}
// Here you have a list of single string-elements { "Item1", "Item2", "Item3", "Item4" } obtained from multiple-string elements { "Item1,Item2", "Item3,Item4" }
// Do what you have to do with them in your DB
hi try这个
hi try this
List<string> list = new List<string>();
foreach (var item in listBox1.Items)
{
list.Add(item.ToString());
}
这样你可以试试。
like this you can try.
这篇关于Foreach一个列表框,拆分值并在Array中插入splitle value的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文