13

Whenever I run my code and try to view a highscore all I get back in my listbox is System.Data.DataRowView.

Can anyone see why?

Code:

MySqlConnection myConn = new MySqlConnection(connStr);
    
string sqlStr = "SELECT CONCAT(Name, ' ', Score) as NameAndScore " + 
                "FROM highscore ORDER BY Score DESC";
    
MySqlDataAdapter dAdapter = new MySqlDataAdapter(sqlStr, myConn);
DataTable dTable = new DataTable();
dAdapter.Fill(dTable);
dAdapter.Dispose();
lstNames.DisplayMember = "NameAndScore";
lstNames.DataSource = dTable;
TylerH
  • 20,799
  • 66
  • 75
  • 101
Cain Neal
  • 197
  • 2
  • 4
  • 15

12 Answers12

21

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.

echavez
  • 847
  • 5
  • 13
7

The following code should work:

DataSet dSet = new DataSet();
dAdapter.Fill(dSet);

lstNames.DisplayMember = "NameAndScore";
lstNames.ValueMember = "NameAndScore";
lstNames.DataSource = dSet.Tables[0];

If it does not work, please update your question and provide us with some information about the columns and values that are actually returned in dSet.Tables[0].

Thorsten Dittmar
  • 55,956
  • 8
  • 91
  • 139
  • not entirely sure how to find the return value for dSet.Tables[0] but when i tested i still got 'Sytem.Data.DataRowView' in the listbox – Cain Neal Mar 15 '13 at 10:26
  • Have you ever debugged? Set a breakpoint to the `lstNames.DisplayMember = ...` line, right click the `dAdapter` word and then quick inspect it. In that window, you can also enter a value like `dAdapter.Tables[0][0]` to see the properties of the first row. – Thorsten Dittmar Mar 15 '13 at 10:34
  • i set the breakpoint but i cannot find an option to inspect dAdapter when i right click? – Cain Neal Mar 15 '13 at 10:43
  • It may be called "Quick Watch" – Thorsten Dittmar Mar 15 '13 at 11:11
5

Set your lstNames.DisplayMember and lstNames.ValueMember fields.

Gets or sets the property to display for this ListControl.


Gets or sets the path of the property to use as the actual value for the items in the ListControl.

This should solve your problem..

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
  • 'System.Windows.Forms.ListBox' does not contain a definition for 'DataTextField' and no extension method 'DataTextField' accepting a first argument of type 'System.Windows.Forms.ListBox' could be found (are you missing a using directive or an assembly reference?) Thats the error i receive when i set the two – Cain Neal Mar 15 '13 at 09:26
  • @CainNeal Your `lstNames` is `ListBox` or not? – Soner Gönül Mar 15 '13 at 09:28
  • This just gives me the System.Data.dataRowView as i got initially, i just asked DarK but could i use a label instead of a listbox? – Cain Neal Mar 15 '13 at 09:40
  • @CainNeal _Well_, I'm not sure your _"how to use adapter"_ or _"which control should I use"_ questions but I'm sure if you want to get real values of your listbox, you should set `DisplayMember` and `ValueMember` properties. – Soner Gönül Mar 15 '13 at 09:48
  • I did set both DisplayMember and ValueMember properties but it gave me the same input to my listbox – Cain Neal Mar 15 '13 at 09:52
  • The values appear to be `DataTextField` and `DataValueField`. There don't appear to be `DisplayMember` or `ValueMember` available in the ASPX side when declaring the asp:ListBox control. – TylerH Jun 09 '22 at 16:29
  • Follow-up; realized I am working in a webform and this is a winform question; that's why I did not see `DisplayMember` or `ValueMember` properties. – TylerH Jun 09 '22 at 19:28
2

Like I said in the comments, please add lstNames.DataBind() to your code.

MySqlConnection myConn = new MySqlConnection(connStr);

string sqlStr = "SELECT CONCAT(Name, ' ', Score) as NameAndScore " + 
                "FROM highscore ORDER BY Score DESC";

MySqlDataAdapter dAdapter = new MySqlDataAdapter(sqlStr, myConn);
DataTable dTable = new DataTable();
dAdapter.Fill(dTable);
dAdapter.Dispose();
lstNames.DisplayMember = "NameAndScore";
lstNames.ValueMember = "NameAndScore";
lstNames.DataSource = dTable;

EDIT:

Can you try this instead:

MySqlConnection myConn = new MySqlConnection(connStr);

    string sqlStr = "SELECT CONCAT(Name, ' ', Score) as NameAndScore " + 
                    "FROM highscore ORDER BY Score DESC";

  myConn .Open();

  SqlCommand cmd = new SqlCommand(sqlStr, SQLConnection1);


      SqlDataReader rd = cmd.ExecuteReader();
      while (rd.Read())
      {
        lstNames.Items.Add(rd[0]);
      }
      rd.Close();
      rd.Dispose();
      myConn.Close();
DarK
  • 221
  • 1
  • 3
  • 16
  • It wont allow me to use .DataBind. It says that lstNames does not contain a definintion, but it will let me use .DataBindings – Cain Neal Mar 15 '13 at 09:30
  • Looks like you're using a ListControl not a ListBox, so can you please just add the valuemember (see edit) and remove the databind? – DarK Mar 15 '13 at 09:35
  • I still only receive 'System.Data.DataRowView' would i be able to use the above code if i used a label instead of a listbox? but obviously change the lstNames – Cain Neal Mar 15 '13 at 09:38
  • Forget about `DataBind`. This does not exist in WinForms and it is not necessary, as obviously data binding is already happening - otherwise you'd not see any items in your list. – Thorsten Dittmar Mar 15 '13 at 09:52
  • For the above code i get a return in the listbox 'Byte[]Array' – Cain Neal Mar 15 '13 at 10:19
1

If you want your list box to show values from an specific column of the data source use

lstNames.DataTextField = "SpecialColumnName";

TylerH
  • 20,799
  • 66
  • 75
  • 101
Emad
  • 3,809
  • 3
  • 32
  • 44
1

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";
Paul Roub
  • 36,322
  • 27
  • 84
  • 93
0

I use :

foreach (DataGridViewRow row in _dataGridView.SelectedRows)
{
    var rowObject = row.DataBoundItem as DataRowView;
    var array = rowObject.Row.ItemArray;
}
Mi-Creativity
  • 9,554
  • 10
  • 38
  • 47
SijeDeHaan
  • 175
  • 2
  • 2
0

I had the similar problem of picking a single value from a populated Listbox and getting System.Data.DataRowView. I was able to solve it by casting the selected item to System.Data.DataRowView and then accessing the Row and ItemArray subclasses:

var si = ListBox1.SelectedItem; 
string val = ((System.Data.DataRowView)si).Row.ItemArray[0].ToString();
roadrunner66
  • 7,772
  • 4
  • 32
  • 38
0

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";
-1

just make sure you are typing the name of field same as in datasource in other word it is a case sensitive

so : Me.GridLookUpEdit1.Properties.DisplayMember = "cur_code" is different than Me.GridLookUpEdit1.Properties.DisplayMember = "CUR_code"

-1

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.

-2

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