C# 从分隔的数据库字符串字段中选择多选列表框中的项目
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/532285/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Selecting items in multi-select listbox from delimited database string field
提问by
I'm trying to build an "edit" page for a database record that can be edited and saved back to the database. One of the fields will be a multi-select listbox that will need to highlight the appropriate list items in a hard-coded list when loaded.
我正在尝试为可以编辑并保存回数据库的数据库记录构建一个“编辑”页面。其中一个字段将是一个多选列表框,加载时需要突出显示硬编码列表中的相应列表项。
Using C#, how do I populate a multi-select listbox -- with the appropriate items selected -- based on the comma-delimited string from a database field? I've researched a few solutions that involve loops, but I have been unable to get them to work with my limited C# skillset.
使用 C#,如何根据数据库字段中的逗号分隔字符串填充多选列表框——选择适当的项目?我研究了一些涉及循环的解决方案,但我一直无法让它们使用我有限的 C# 技能集。
This is all I have now, before I got stuck. You'll see that it doesn't account for multiple values in the string. Is there a function like "contains" that I can check to see if the value matches? I'm still missing some (probably basic) C# logic and coding here.
这就是我现在所拥有的,在我被卡住之前。您会看到它没有考虑字符串中的多个值。是否有像“包含”这样的函数可以检查值是否匹配?我在这里仍然缺少一些(可能是基本的)C# 逻辑和编码。
int i;
for (i = 0; i <= CATEGORYListBox.Items.Count - 1; i++)
{
if (reader["CATEGORY"].ToString() == CATEGORYListBox.Items(i).Value)
{
CATEGORYListBox.Items(i).Selected = True;
}
}
...
...
<asp:ListBox ID="CATEGORYListBox" runat="server">
<asp:ListItem Value="Circulation">Circulation</asp:ListItem>
<asp:ListItem Value="Interactive Media">Interactive Media</asp:ListItem>
<asp:ListItem Value="Classified">Classified</asp:ListItem>
<asp:ListItem Value="Publishing">Publishing</asp:ListItem>
<asp:ListItem Value="Editorial">Editorial</asp:ListItem>
<asp:ListItem Value="Retail">Retail</asp:ListItem>
</asp:ListBox>
Thanks everyone.
谢谢大家。
采纳答案by Michael Meadows
This is brute force and ugly, but it should work. It looks like your code above is some sort of hybrid between VB and C#. The code below is C# only. Also, consider not doing your ADO.Net inyour codebehind.
这是蛮力和丑陋,但它应该工作。上面的代码看起来像是 VB 和 C# 之间的某种混合。下面的代码仅适用于 C#。另外,请考虑不要在代码隐藏中执行 ADO.Net 。
for (int i = 0; i < CATEGORYListBox.Items.Count; i++)
{
foreach (string category in reader["CATEGORY"].ToString().Split(','))
{
if (category != CATEGORYListBox.Items[i].Value) continue;
CATEGORYListBox.Items[i].Selected = true;
break;
}
}
回答by Jab
I would suggest something along these lines. It seems more readable than doing nested loops.
我会建议一些类似的东西。它似乎比嵌套循环更具可读性。
List<string> categories = new List<string>(reader["CATEGORY"].ToString().Split(','));
foreach (ListItem item in CATEGORYListBox.Items)
{
if (categories.Contains(item.Value))
item.Selected = true;
}
回答by Jab
Another Solution for the problem is:
该问题的另一个解决方案是:
string[] arrSplitItems;
arrSplitItems = TestsOrdrd.Split(',');
if (arrSplitItems.Length > 0)
{
for (int iCount = 0; iCount < arrSplitItems.Length; iCount++)
{
lstTestcode.Items.FindByValue(arrSplitItems[iCount].ToString()).Selected = true;
}
}
TestsOrdrd contains the Selected values of Listbox.
TestsOrdrd 包含 Listbox 的 Selected 值。
Thanks, Rathika Krishnavelu
谢谢, Rathika Krishnavelu
回答by Vaclav Elias
The easiest implementation?
最简单的实现?
string sequenceFromDBorPostBack= "1,3,4,6,48";
foreach (ListItem item in lstLocations.Items)
{
item.Selected = sequenceFromDBorPostBack.Split(',').Contains(item.Value);
}
I am not sure if this is performance effective..
我不确定这是否对性能有效..
回答by user2480323
ListBox1.SelectionMode = ListSelectionMode.Multiple;
string[] pageRoles = myReader["UserProfile"].ToString().Split(',').ToArray();
pageRoles.ToList().ForEach(r => ListBox1.Items.FindByValue(r).Selected = true);