Click here to Skip to main content
16,016,580 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have this code which sends items from a cell in the database which is in listform to a lsitbox.
But when it loads in the database it appears in a long list rather than each new item going to a new line..

Can any one help?


private void ItemCBx_SelectedIndexChanged(object sender, EventArgs e)
{

    Listbox1.Items.Clear();
    MAcon.Open();
    OleDbDataAdapter da = new OleDbDataAdapter("Select Process from [Product Family] Where [Product Name] = @ProductName", MAcon);
    da.SelectCommand.Parameters.AddWithValue("@ProductName", ItemCBx.SelectedItem.ToString());
    DataTable dtbl = new DataTable();
    da.Fill(dtbl);

    if (dtbl.Rows.Count == 1)
    {
        Listbox1.Items.Add(dtbl.Rows[0][0]);
    }
    MAcon.Close();
    Listbox1.Show();
    Listbox1.Text.ToString().Split(',').ToList().ForEach(r => Listbox1.Items.Add(r.Trim()));
    //           Define.SelectedIndex = 1;
}


What I have tried:

Listbox.Text = Listbox1.Text.Replace(",", Environment.NewLine);
Posted
Updated 17-May-18 22:15pm

1 solution

C#
Listbox1.Text.ToString().Split(',').ToList().ForEach(r => Listbox1.Items.Add(r.Trim()));

Statements like that are great until they don't work. Separate it into its constituent parts and use your debugger to see exactly what is happening. I would also suggest that this code should be inside the preceding if clause.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900