none
Listbox Binding

    Question

  • Hi All,

        I am storing mutiple selected items indexes of a listbox in the database.

    When I am trying to bind those indexes to the Listbox only the last indexed item is highlighting.

    for (inti = 0; i < measure.Length; i++){

    Measures.SelectedIndex =Convert.ToInt16(measure[i]);

    }

     

    measure is the array contianing the indexes. Measures is the Listbox.

    measure[0]= 1, measure[1] = 4 . Only 4th index values is binding

    Help me to bind all the selected indexes.

     

    Thanks in Advance

    Friday, April 27, 2012 5:23 AM

Answers

  • hi ,

     i was just giving an example , so i kept it in that for loop.   You need to write it like this .

      I am assuming your Listbox name is Measures ,  and the array where you stored the selected indexes is Measure

      so Measure[0] =1 , Measure[1] =4

      You need to get Measures.Items[1] and Measures.Items[4]  , right.

      So you need to change that to

      Measures.SelectedItems.Add(  Measures.Items[Measure[i]]   );      

     Hope it Helps    Laughing

    Friday, April 27, 2012 7:00 AM

All replies

  • Hi,

               Instead of using SelectedIndex, you need to use the SelectedItem  collection

                listBox1.ItemsSource = "1 2 3 4 5 6 7 8".Split(' ');
                listBox1.SelectedItems.Add(listBox1.Items[2]);
                listBox1.SelectedItems.Add(listBox1.Items[4]);
                listBox1.SelectedItems.Add(listBox1.Items[6]);

             I just showed an example of how to select multiple items.  So in your code you can write something like this

             for(int i=0;i<measure.Length;i++)

             {       Measure.SelectedItems.Add(Measure.Items[i]);         }

    Hope it Helps Laughing

    Friday, April 27, 2012 5:56 AM
  • Thanks Kiran, but there is a small problem again,

    measure[0] = 1

    measure[1] = 4

    I used the code which is given by you, it is selecting the first two values, but I need to select second and fifth items bcoz the indexes are 1 and 4.

     

    Thanks,

    Friday, April 27, 2012 6:45 AM
  • hi ,

     i was just giving an example , so i kept it in that for loop.   You need to write it like this .

      I am assuming your Listbox name is Measures ,  and the array where you stored the selected indexes is Measure

      so Measure[0] =1 , Measure[1] =4

      You need to get Measures.Items[1] and Measures.Items[4]  , right.

      So you need to change that to

      Measures.SelectedItems.Add(  Measures.Items[Measure[i]]   );      

     Hope it Helps    Laughing

    Friday, April 27, 2012 7:00 AM