leewebdev
leewebdev

Reputation:

Selecting items in multi-select listbox from delimited database string field

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.

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.

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.

Upvotes: 2

Views: 19039

Answers (5)

user2480323
user2480323

Reputation: 1

ListBox1.SelectionMode = ListSelectionMode.Multiple;
string[] pageRoles = myReader["UserProfile"].ToString().Split(',').ToArray();
pageRoles.ToList().ForEach(r => ListBox1.Items.FindByValue(r).Selected = true);

Upvotes: 0

Rathika
Rathika

Reputation:

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.

Thanks, Rathika Krishnavelu

Upvotes: 0

Vaclav Elias
Vaclav Elias

Reputation: 4151

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..

Upvotes: 0

Michael Meadows
Michael Meadows

Reputation: 28416

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 in your codebehind.

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;
    }
}

Upvotes: 2

Jab
Jab

Reputation: 13775

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;
    }

Upvotes: 5

Related Questions