0

I just started learning C# yesterday. I am working with windows form. I could retrieve data from database and display them in combo box list. But for data update, the combo.SelectedValue does not work. I have tried combo.selectedItem, but still, it does not work. What should i do?

selectedDescription = dr["value"].ToString().Split(',').ToList<string>(); 

if (dr["type"].ToString() == "multiple") {
    ComboBox combo = new ComboBox();
    combo.DataSource = selectedDescription;
    combo.Width = 60;
    combo.Height = 27;
    combo.Location = new Point(currentX, currentY);
    combo.Tag = Id;

Code to retrieve data is as follows:

if (getVal != null) {
    foreach (DataRow ds in getVal.Tables[0].Rows) {
        if (Convert.ToInt32(ds["descId"].ToString()) == combo.Tag) {
            combo.SelectedValue = ds["val"].ToString();
        }

I've tried something like this, but it doesn't work either:

List<string> str = (List<string>)combo.DataSource;
str.Add(ds["val"].ToString());
combo.DataSource = null;
combo.DataSource = str;
combo.Text = ds["val"].ToString();
pizzaisdavid
  • 455
  • 3
  • 13
  • @PranavSingh can you help me with this? – ElaineCottrell May 11 '15 at 03:48
  • Are you trying to set the selected combo item based on what you are pulling from the database, or are you trying to get the value the user has selected from the combo? – Brent Mannering May 11 '15 at 04:28
  • @BrentMannering, I am trying to set the item based on the data i fetch from the database. But i also need to maintain the combo box list. Lets say the original combo list ={ 1, 2, 3} and user pick 2 as the value and save it into the database. So, if the user wants to update the data, the selectedItem will be 2 and 1 and 3 still on the list. – ElaineCottrell May 11 '15 at 05:40
  • It's not obvious to me why, your code wouldn't be working. Perhaps try some of the following ideas from here: http://stackoverflow.com/questions/6688157/how-to-set-selected-value-from-combobox – Brent Mannering May 11 '15 at 20:59
  • @BrentMannering I think it has to do with the dotnet version im using. I'm using version 3.5 – ElaineCottrell May 12 '15 at 00:52

0 Answers0