listbox selected items

I have 3 listboxes that are being filled with same data and from 1 store procedure call to the database.
Theorecctially, the user could selected an item from each listbox, however, when doing so, the other 2 listboxes change to have the same selected item.

Ex - If I select item A in listbox1, then listbox2 and listbox3 show their selected item as A. 
  Then if I go and selected item C from listbox2, listbox 1 & listbox3 change their selected item to C as well.

Does anyone know what I am doing wrong

Thanks.


Answer this question

listbox selected items

  • carlisle

    It is because your listboxes have the same binding context.  Check out this link:

    http://msdn.microsoft.com/library/default.asp url=/library/en-us/cpref/html/frlrfsystemwindowsformsbindingcontextclassctortopic.asp

    Tony

  • P K Agarwal

    I resolved this by filling a dataset and assigning it to a DataTable, then I copied that DataTable to other DataTables.
    So its working properly now.


  • kanuhelp

    If that's how you want it, fine.  But your consuming three times the memory using separate DataTables.  Also, if something changed in one of them you would have to be sure to copy those changes to the other table also.  As I stated before, what you want can be easily accomplished by creating a different binding context for each listbox.  Check out the link, if you want.

    Tony

  • iiiwabibitoiii

    Strange, in principle listboxes are all independed, though it might be that if you merely point to the value you get from the database they are connected through these. I therefore propose you have the items of listbox2 and listbox3 point to a copy of the list of listbox1. Without code it is merely quesswork of what is wrong, I hope this helps.
  • Dave Midgley

    Excellent guess!!  I think that is the problem.

    Here is the code - although, if I point to a copy of the list of listbox1, will I still be able to capture the value selected in listbox2 and lisbox3 to be able to send it back to the database for another query  
    I really don't want to have to connect to the database for each listbox, because one option allows them to have up to 10 options in the listbox and 10 calls would greatly slow performance.

    Here is what I'm trying to do:

    private void GetState()
    {
    try
    {  
    dvState = new DataView(SqlHelper.ExecuteDataset(cnIPDAdmin, CommandType.StoredProcedure, "gsp_State").Tables[0]);
    }
    catch (Exception err)
    {
    MessageBox.Show(err.ToString() + "\n ERROR State " );
    }
    }//end of GetState()


    private void cbxStateSqlComp_SelectedIndexChanged(object sender, System.EventArgs e)
    {
    if(loaded == true)
    {
    if(cbxStateSqlComp.SelectedIndex == 0)

    gbxStateEqual.Visible = true;
    selectedOp = "lbxBtwn";

    lbxStateEqual1.DataSource = dvState;
    lbxStateEqual1.DisplayMember = "State";
    lbxStateEqual1.ValueMember = "State";

    lbxStateEqual2.DataSource = dvState;
    lbxStateEqual2.DisplayMember = "State";
    lbxStateEqual2.ValueMember = "State";

    lbxStateEqual3.DataSource = dvState;
    lbxStateEqual3.DisplayMember = "State";
    lbxStateEqual3.ValueMember = "State";

    }


    Thanks for your help on this!!



  • listbox selected items