2

I need to get values from selected items from listbox which is binding from database. But if I try

listBoxAtribute.SelectedItems[0].ToString()

it returns System.Data.DataRowView

Is there any way to convert Data from DataRowView to string?

My idea looks like this:

for(int i = 0; i < listBoxAtribute.SelectedItems.Count; i++)
{
  MessageBox.Show(listBoxAtribute.SelectedItems[i].Tostring);
}

Thanks a lot for any reply.

akjoshi
  • 15,374
  • 13
  • 103
  • 121
BeerMaker
  • 41
  • 5

4 Answers4

0

Try casting selected item of listbox to DataRowItem first and access columns value passing column name to indexer

Here is the sample

((DataRowView)Listbox.SelectedItem)["<column_name>"].ToString();
user968441
  • 1,471
  • 9
  • 22
  • 48
0

If you want to show fist column value, then take fist item of listBoxAtribute.SelectedItem as

for(int i = 0; i < listBoxAtribute.SelectedItems.Count; i++)
{
  MessageBox.Show(listBoxAtribute.SelectedItems[i].Item[0]); 
}
Damith
  • 62,401
  • 13
  • 102
  • 153
0
string[] items = listBoxAtribute.SelectedItems.Select(x => x.Item[0]);    
Asif Mushtaq
  • 13,010
  • 3
  • 33
  • 42
0

Under the assumption that you are seeing, visibly that is, a single specific column from said database driven entries, that would indicate that you have the DisplayMember property of the listbox set. Possibly the ValueMember as well, assuming you are using EditValue anywhere. I would write something along these lines.

((DataRowView)listBoxAtribute.SelectedItems[0])[listBoxAtribute.DisplayMember].ToString();

That way you get exactly what the user would see on the screen. Now, if you want a different piece of data than the one shown on the screen, you would need to use user968441's approach and hard code the column name. But that's also relatively easy.

Nevyn
  • 2,623
  • 4
  • 18
  • 32