Why I get “System.Data.DataRowView” instead of real values in my Listbox?

前端 未结 12 762
一向
一向 2020-11-29 08:24

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.

相关标签:
12条回答
  • 2020-11-29 09:18

    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";
    
    0 讨论(0)
  • 2020-11-29 09:20

    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.

    0 讨论(0)
  • 2020-11-29 09:25

    Sometimes what might cause it is mis-spelt displaymember variable name.

    0 讨论(0)
  • 2020-11-29 09:26

    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.

    0 讨论(0)
  • 2020-11-29 09:30

    I use :

    foreach (DataGridViewRow row in _dataGridView.SelectedRows)
    {
        var rowObject = row.DataBoundItem as DataRowView;
        var array = rowObject.Row.ItemArray;
    }
    
    0 讨论(0)
  • 2020-11-29 09:31

    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";
    
    0 讨论(0)
提交回复
热议问题