1

First of all i am very embarrassed to be asking because it seems so simple but ive been scouting the forums and just haven't got my head around how this works.

I am trying to populate my list box from a table from SQLite, But i need the actual value to be the id from the database and not the shown value. Now i know there are alot of already answered questions on here but none of the answers ive read seem to work for me.

I am coming over from php programming and it was very simple to do there.

The code i am using is

    private void fillTheColours(ListBox colourListBox)
    {
        colourListBox.Items.Clear();
        con.Open();
        SQLiteCommand command = new SQLiteCommand(con);
        command.CommandText = "SELECT colourId,suffix FROM colours ORDER BY suffix ASC";
        command.ExecuteNonQuery();

        SQLiteDataReader reader = command.ExecuteReader();

        while (reader.Read())
        {
            colourListBox.Items.Add(reader["suffix"]);
        }
        con.Close();
    }

That is my function and it works nice to put the visible values into the listbox but that value is useless in the next step of the application.

First of all how would i add the id. Second how would i then access that id ? To set it to a variable ?

Just so its known im using c# in a windows form application in visual studio 2015

Cheers in advance Ryan

Hadley8899
  • 136
  • 1
  • 12
  • http://stackoverflow.com/questions/867514/make-listbox-items-have-a-different-value-than-item-text – User1234 May 08 '16 at 11:36
  • `ListBox.DisplayMember = "MyKey"; // the display value ListBox.ValueMember = "MyValue"; // can be id.` – Berkay Yaylacı May 08 '16 at 11:38
  • Ive seen that answer before. But i will need a full explanation to how to use it all, Ive been trying for about 2 days and i just cant get my head around it. – Hadley8899 May 08 '16 at 12:09

2 Answers2

0
colourListBox.Items.add(new ListBoxItem("name", "value"));

Change code to add item as above.

To get selected ID use below code:

string id = colourListBox.SelectedValue;

Alternate method:

    private void fillTheColours(ListBox colourListBox)
     { 
    colourListBox.Items.Clear();
 SQLiteDataAdapter  da = new SQLiteDataAdapter("SELECT colourId,suffix FROM colours ORDER BY suffix ASC",con); 
    DataSet ds = new DataSet();
    da.Fill(ds);

    colourListBox.ValueMember= "colourId";
    colourListBox.DisplayMember="suffix";
    colourListBox.DataSource = ds;
     }
Akshey Bhat
  • 8,227
  • 1
  • 20
  • 20
  • The type or namespace name 'ListBoxItem' Could not be found. This is where im struggling as ive seen that answer before but it never worked. After all the reading ive done on the topic ill need a full explanation to get my head around it, Ive been trying to 2 days to get it to work and im so embarassed as it seems its such a simple thing to do. – Hadley8899 May 08 '16 at 12:08
  • Add using System.Windows.Controls; to code. This will add namespace required. – Akshey Bhat May 08 '16 at 12:12
  • The namespace controls does not exist in the namspace System.Windows . This is becoming a big headache for me. I am using .NET framework 4.5.2 if that makes any difference ? – Hadley8899 May 08 '16 at 12:17
  • There might be the case of mismatch between versions of DLL referenced and your project version.http://stackoverflow.com/questions/4764978/the-type-or-namespace-name-could-not-be-found – Akshey Bhat May 08 '16 at 12:23
  • I added a reference to System.Windows and still dont have controls, Although in the reference manager i have access to System.Windows.Controls.Ribbons, Strange – Hadley8899 May 08 '16 at 12:36
  • Check the link in my last comment. It might help you. – Akshey Bhat May 08 '16 at 12:40
  • Well ive managed to get System.Windows.Controls in, Now every reference to the listBox now wants to be System.Windows.Controls.ListBox and my fill The Colours method is moaning because it cant convert from a windows form listbox to a window.Controls list box Is there not an easier way to do this ? Surely it cant be this difficult ? – Hadley8899 May 08 '16 at 14:34
  • Sorry for the late reply, Just got back home, Ive tried your updated method and i get and ArgumentException was unhandled on the colourListBox.ValueMember = "colourId", Additional information: cannot bind to the new display member. I had tried this method before and still got this message. Im really confused – Hadley8899 May 08 '16 at 21:17
  • try setting datasource after you have set DisplayMember and ValueMember properties. Check the modified answer. – Akshey Bhat May 09 '16 at 03:59
  • Thankyou for your patience, Ive got it working now with a datatable rather than a datasource, I will accept your answer. Cheers the help – Hadley8899 May 09 '16 at 09:30
0

while (reader.Read()) {

         string id  =reader["colourId"].ToString();
         string suf =reader["suffix"].ToString();
         var newit  = new ListBoxItem(id, suf);
         colourListBox.Items.add(newit);


    }
Yasser Gersy
  • 93
  • 1
  • 7