c# - How to put combobox Items in a list? -


i added items combobox using:

sqldatareader sqlreader = sqlcmd.executereader(); while (sqlreader.read()) {     string name = sqlreader.getstring(0);     combobox1.items.add(name); } sqlreader.close(); conn.close(); 

now want put these value in string list. possible , how can that?

simply can like

string[] items = new string[combobox1.items.count];     for(int = 0; < combobox1.items.count; i++)    {        items[i] = combobox1.items[i].tostring();    } 

or if want create string list directly reader object

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 make job easier

var arr = combobox1.items.cast<object>()       .select(item => item.tostring()).toarray(); 

Comments

Popular posts from this blog

sql - invalid in the select list because it is not contained in either an aggregate function -

Angularjs unit testing - ng-disabled not working when adding text to textarea -

How to start daemon on android by adb -