Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

wrong output when using a query to populate comboBox

Tags:

c#

Im trying to populate a comboBox with the output of a query. What I get displayed in the comboBox is System.Data.DataRowView Here's the code that I use:

string subConStr = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\Master.accdb;Jet OLEDB:Database Password=password";
       string query = "SELECT DISTINCT Code FROM MasterTable";
       OleDbDataAdapter dAdapterComB = new OleDbDataAdapter(query, subConStr);
       System.Data.DataTable source = new System.Data.DataTable() ;
       dAdapterComB.Fill(source);
       comboBoxSubject.DataSource = source;
       comboBoxSubject.DisplayMember = "Subjects";
like image 359
Silvia Stoyanova Avatar asked Feb 27 '26 02:02

Silvia Stoyanova


2 Answers

There is no column "Subjects" in your data table, so the combo box doesn't know which field to use to display the value in the combo box. When it can't find the member it just displays .ToString on thew DataRowView

You can try:

comboBoxSubject.DisplayMember = "Code";
like image 129
Nick Bray Avatar answered Feb 28 '26 17:02

Nick Bray


Change the display member to "Code"

like image 22
humblelistener Avatar answered Feb 28 '26 15:02

humblelistener