C # datasource returns System.Data.DataRowView

I have a combobox cbAnalytes:

cbAnalytes.DataSource = ConnectandReadList(qcvalues_query); 

where is the ConnectandReadList:

 public DataTable ConnectandReadList(string query) { DataTable ds = new DataTable(); string connection_string = "Data Source=hermes;database=qcvalues; Integrated Security=SSPI;"; using (var myConnection = new SqlConnection(connection_string)) { myConnection.Open(); var command = new SqlCommand(query, myConnection); var adapter = new SqlDataAdapter(command); adapter.Fill(ds); } return ds; } 

for some reason, it fills in the combo box:

 System.Data.DataRowView System.Data.DataRowView System.Data.DataRowView System.Data.DataRowView System.Data.DataRowView 

Does anyone know what I'm doing wrong?

+4
source share
6 answers

What do you set for the DisplayMember and ValueMember ? You need to set these properties for your lists so that it knows what to display.

+14
source

For ASP.net:

 cbAnalytes.DataValueField = "ColumnName"; cbAnalytes.DataTextField = "ColumnName"; 

For Windows Forms:

 cbAnalytes.DisplayMember = "ColumnName"; cbAnalytes.ValueMember = "ColumnName"; // don't set this if you want the Value to be the DatRowView itself 
+1
source

Do you set the Combibox DisplayMember and ValueMember properties to columns of text and identifier?

http://www.codeproject.com/KB/database/scomlistcontrolbinding.aspx

+1
source

In short, you need to bind the properties of the displayed item (DisplayMember and ValueMember, as indicated by @AJ) to the properties of your DataTable rows (i.e. the columns that you want to use for use with the list). He is currently blindly trying to display the object provided as the cbo data source. He does this by calling object.ToString() , which, if not overridden, tends to return a type name.

+1
source

your combo box should look something like this.

 <cc1:ComboBox ID="cbAnalytes" DataTextField="DATAROWPROPERTYHERE" DataValueField="DATAROWPROPERTYHERE" runat="server" > </cc1:ComboBox> 
0
source

You can assign field names that differ from the result set returned by the adapter / data reader or the select statement. Pls check the field names.

0
source

All Articles