?
Solved

Selecting Multipul Items in the List Box

Posted on 2009-12-31
6
Medium Priority
?
303 Views
Last Modified: 2012-05-08
Dear All,
I have the ListBox which is beinging populated from database;
i am using following query Select 'AB' As 'Units' union  
       Select 'BC' As 'Units' union  
       Select 'DE' As 'Units' union  
       Select 'EF' As 'Units' union  
       Select 'FG' As 'Units'       
I have enabled Selection mode as MultiExtended.
In the table i am storing the values like DE||EF.
On the Load DE and EF should be selected . Please check the following code.
//--lstBusinessUnit is the list box.
string SelectedNames = dsTasksDetails.Tables[0].Rows[0]["Units"].ToString();
                        string[] Names = SelectedNames.Split(new string[] { "||" }, StringSplitOptions.RemoveEmptyEntries);
foreach (string Name in Names)
                        {
                            for (int iLoop = 0; iLoop < lstBusinessUnit.Items.Count; iLoop++)
                            {
                                if (((System.Data.DataRowView)(lstBusinessUnit.Items[iLoop])).Row.ItemArray[0].ToString() == Name)
                                {
                                    //lstBusinessUnit.SetSelected(iLoop, true);
                                    lstBusinessUnit.SelectedItem = Name;
                                    break;
                                   
                                }
                            }

                        }

the following code is not working.
Please help in this.
0
Comment
Question by:UJayaPrakashRao
  • 4
  • 2
6 Comments
 
LVL 10

Expert Comment

by:lof
ID: 26155925
Hey,

you don't say what the problem exactly is except that <<it's not working>>
you don't specify how do you populate data from DB and it may be important

but for now have a look on the sample code i produced

just create a form and add a ListBox onto it, with a standard name. than place my code in Form1_Load
private void Form1_Load(object sender, EventArgs e)
        {
            listBox1.Items.Add("AB");
            listBox1.Items.Add("BC");
            listBox1.Items.Add("CD");
            listBox1.Items.Add("DE");
            listBox1.Items.Add("EF");
            listBox1.Items.Add("FG");

            listBox1.SelectionMode = SelectionMode.MultiSimple;

            List<string> selectedNames = new List<string>();
            selectedNames.Add("CD");
            selectedNames.Add("EF");

            //you cannot change items when enumerating 
            //so you need to use temporary object list
            List<object> toBeSelected = new List<object>();
            //check all items in the list
            foreach (object obj in listBox1.Items)
                //if obj is matching the one you are looking for
                if (selectedNames.Contains(obj.ToString()))
                    //add it to the temporary table
                    toBeSelected.Add(obj);                  

            //now for every object in temporary list
            foreach (object obj in toBeSelected)
                //select it in the list
                listBox1.SelectedItems.Add(obj);

        }

Open in new window

0
 
LVL 10

Expert Comment

by:lof
ID: 26155975
I have read your code for the third time and I have noticed something more.

The reason your code is not working is because you are using DataSource property to bind ListBox object to a DataTable.

I have changed your original code, see attached.

Oh, and by the way the problem is not with the database but with C# :)
string SelectedNames = dsTasksDetails.Tables[0].Rows[0]["Units"].ToString();
            List<String> names = SelectedNames.Split(new string[] { "||" }, StringSplitOptions.RemoveEmptyEntries).ToList();

            List<DataRow> rowsToBeSelected = new List<DataRow>();
            foreach (object obj in lstBusinessUnit.Items)
            {
                DataRow dr = ((System.Data.DataRowView)(obj)).Row;
                if (dr.ItemArray[0].ToString() == Name)
                {
                    rowsToBeSelected.Add(dr);
                    break;
                }
            }

            foreach (DataRow dr in rowsToBeSelected)
                lstBusinessUnit.SelectedItems.Add(dr);

Open in new window

0
 

Author Comment

by:UJayaPrakashRao
ID: 26165424
Dear lof,

Thanks for the reply,
The Problem with the code is in the list Box the values  DE and EF should be selected.
But by using my code it is not selecting this values.

I have made changes to my code as per ur's still it is not working.

Please find the below modified code.
lstBusinessUnit.SelectionMode = SelectionMode.MultiSimple;
                        string SelectedNames = dsTasksDetails.Tables[0].Rows[0]["BusinessUnit"].ToString();
                        string[] Names = SelectedNames.Split(new string[] { "||" }, StringSplitOptions.RemoveEmptyEntries);
                        List<string> selectedNames = new List<string>();
                        foreach (string Name in Names)
                        {
                            selectedNames.Add(Name);
                        }
                        List<object> toBeSelected = new List<object>();
                        foreach (object obj in lstBusinessUnit.Items)
                        {
                            DataRow dr = ((System.Data.DataRowView)(obj)).Row;
                            if (selectedNames.Contains(dr.ItemArray[0].ToString()))
                            {
                                toBeSelected.Add(dr);
                                //break;
                            }

                        }
                        foreach (object obj in toBeSelected)
                            //select it in the list
                            lstBusinessUnit.SelectedItems.Add(obj);

Please help in this

0
Granular recovery for Microsoft Exchange

With Veeam Explorer for Microsoft Exchange you can choose the Exchange Servers and restore points you’re interested in, and Veeam Explorer will present the contents of those mailbox stores for browsing, searching and exporting.

 
LVL 10

Accepted Solution

by:
lof earned 2000 total points
ID: 26165531
Sorry, my mistake. Have a look at this revised code.
lstBusinessUnit.SelectionMode = SelectionMode.MultiSimple; 
string SelectedNames = dsTasksDetails.Tables[0].Rows[0]["BusinessUnit"].ToString();
string[] Names = SelectedNames.Split(new string[] { "||" }, StringSplitOptions.RemoveEmptyEntries);
List<string> selectedNames = Names.ToList();
List<object> toBeSelected = new List<object>();
foreach (object obj in lstBusinessUnit.Items)
{
   DataRow dr = ((System.Data.DataRowView)(obj)).Row;
   if (selectedNames.Contains(dr.ItemArray[0].ToString()))
   {
        toBeSelected.Add(obj);
   } 
}
lstBusinessUnit.ClearSelected();
foreach (object obj in toBeSelected)
    //select it in the list 
    lstBusinessUnit.SelectedItems.Add(obj);

Open in new window

0
 

Author Comment

by:UJayaPrakashRao
ID: 26169003
Dear lof,

Still it is not working. 'AB' value is getting selected rather than selecting DE and EF.
0
 
LVL 10

Expert Comment

by:lof
ID: 26188145
Dear UJayaPrakash,

AB is selected by default so if it is the only item selected it suggests that nothing is selected. Fair guess would be that even the code in line 14 is not exectued as it should clear all the selection.

Please try debugging the solution and follow the code flow. Make sure the records are added to toBeSelected list - the problem may be with comparing strings.

Try creating a simple, very simple example project and add the code and compare the two.

I keep an eye on the thread so keep me informed

Lof
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
In real business world data are crucial and sometimes data are shared among different information systems. Hence, an agreeable file transfer protocol need to be established.
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question