Little Programmer
Little Programmer

Reputation: 191

How to put combobox Items in a list?

I added Items to a combobox using:

SqlDataReader sqlReader = sqlCmd.ExecuteReader();
while (sqlReader.Read())
{
    string name = sqlReader.GetString(0);
    combobox1.Items.Add(name);
}
sqlReader.Close();
conn.Close();

Now I want to put these value in a string list. Is that possible and how can I do that?

Upvotes: 10

Views: 19372

Answers (5)

I'm sure there's a more elegant method, but this works

List<string> values = this.ComKeyType.Items
                      .Cast<object>()
                      .Select(x => x.ToString().Split('=')[1].Replace(" }", "").Trim())
                      .ToList();

Upvotes: 1

mechanic
mechanic

Reputation: 781

You would really need to separate concerns in your code to make it maintainable.

If you want to bind a list to combobox, you can do it in XAML:

<ComboBox ItemsSource="{Binding MyList}" SelectedItem="{Binding CurrentItem}" />

You'd better make an ObservableCollection property in your view model:

public ObservableCollection<string> MyList {get; private set;}

and initialize it in your view model's constructor:

MyList = new ObservableCollection<string>(GetNames());

GetNames() here is the method where your SQL code is incapsulated:

private List<string> GetNames() {
    var myList = new List<string>();
    SqlDataReader sqlReader = sqlCmd.ExecuteReader();
    while (sqlReader.Read())
        {
            string name = sqlReader.GetString(0);
            myList.Add(name);
        }
    sqlReader.Close();
    conn.Close();

    return myList;
}

Upvotes: 0

Rahul Hendawe
Rahul Hendawe

Reputation: 912

You probably want this:

List<String>  cbList = new List<String>();

var lstItems= ComboBox1.Items.Cast<Object>()
          .Select(item => item.ToString()).ToList();

cbList.Add(lstItems); 

Upvotes: 0

MANISH KUMAR CHOUDHARY
MANISH KUMAR CHOUDHARY

Reputation: 3492

Simply you can do something like

string[] items = new string[combobox1.Items.Count];  

 for(int i = 0; i < combobox1.Items.Count; i++)
   {
       items[i] = combobox1.Items[i].ToString();
   }

Or if want to Create a string list directly from reader object than

var itemList=new List<string>();
SqlDataReader sqlReader = sqlCmd.ExecuteReader();
                while (sqlReader.Read())
                {

                    string name = sqlReader.GetString(0);
                    combobox1.Items.Add(name);
                    itemList.Add(name);
                }
                sqlReader.Close();
                conn.Close();
            }

Use of LINQ will make you job very easier

var arr = combobox1.Items.Cast<Object>()
      .Select(item => item.ToString()).ToArray();

Upvotes: 10

Brendon
Brendon

Reputation: 334

private List<string> ComboBoxList = new List<string>();

Create this outside of the method you are currently in. This List will allow you to use it in any method within the class.

private List<string> ComboBoxList;

or Try this instead of the top piece of code. They both work.

SqlDataReader sqlReader = sqlCmd.ExecuteReader();
                while (sqlReader.Read())
            {

                string name = sqlReader.GetString(0);
                combobox1.Items.Add(name);
                comboBoxList.Add(name);
            }
            sqlReader.Close();
            conn.Close();
        }

Create a new list and add each name to the list.

Upvotes: 2

Related Questions