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

前端 未结 12 761
一向
一向 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:06

    If you want your list box to show values from an specific column of the data source use lstNames.DataTextField = "SpecialColumnName";

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

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

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

    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].

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

    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"

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

    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..

    0 讨论(0)
提交回复
热议问题