I\'m hoping someone can help. But whenever I run my code and try to view a highscore
all I get back in my listbox is System.Data.DataRowView
.
I know its an old question. Just thought it might be nice to add an update to the answers given already. I was looking for a solution to the same problem. I the realized it is in which order you place these 3 lines of code
lstNames.DisplayMember = "NameAndScore";
lstNames.ValueMember = "NameAndScore";
lstNames.DataSource = dTable;
It should use the datasource first. Then The value member and then the display member As follow:
lstNames.DataSource = dTable;
lstNames.ValueMember = "NameAndScore";
lstNames.DisplayMember = "NameAndScore";
if you copied the object instead of placing as a new object it will do this. Try deleting the object and putting it back on. I had the same issue and this is what I did. Don't know why it worked, but it did.
Sometimes what might cause it is mis-spelt displaymember variable name.
I always have to deal with this problem, even if I set the DisplayMember
and ValueMembers
of the List Box.
Your current code is correct and should work, if you need access to the current selected item value of any column of your dTable
you can get them doing this:
DataRowView drv = (DataRowView)lstNames.SelectedItem;
String valueOfItem = drv["NameAndScore"].ToString();
What I like about getting the entire DataRowView
is that if you have more columns you can still access their values and do whatever you need with them.
I use :
foreach (DataGridViewRow row in _dataGridView.SelectedRows)
{
var rowObject = row.DataBoundItem as DataRowView;
var array = rowObject.Row.ItemArray;
}
Only the order of the calls is wrong. First set the DataSource
and only after that set the display member:
lstNames.DataSource = dTable;
lstNames.DisplayMember = "NameAndScore";