Nilaksha Perera
Nilaksha Perera

Reputation: 725

how to load data table's column values to listbox?

I have a data table variable called _dataTable which contains a data table which holds a single column . How can I load the _dataTable's row values to a ListBox without using the DataSource property ? I have to manipulate the data in the ListBox later on so using a DataSource don't allow user to edit the ListBox. Code examples will be appreciated .

Thank you

Upvotes: 0

Views: 7446

Answers (2)

Sharique Ansari
Sharique Ansari

Reputation: 544

   string yourvalue = _dataTable.Rows[0].ItemArray[0]

Should do it

Indexes are 0 based so we first access the first row (0) and then the 1th column in the row (0)

or

you can also use this:-

string yourvalue =  _dataTable.Rows[0]["column name"].ToString();

Add this value to your list box, i am assuming listbox1 is the id of your listbox

listbox1.Items.Add(new ListItem(yourvalue));

Upvotes: 1

Tim Schmelter
Tim Schmelter

Reputation: 460018

If you don't want to use the DataSource you can fill it manually:

foreach(DataRow row in _dataTable.Rows)
    listBox.Items.Add(row[0]);

But even if you use the DataSource you could modify the DataTable later.

Upvotes: 4

Related Questions