Skip to content

Is it possible to populate a c# list box with a Row of data from sql server

So, I am new to C#, Visual studio etc, however I have a bit of experience with SQL Server. I Have a table, that contains Data (Obviously), and what I am trying to do is retrieve that data and display it in a drop down list so that a client can select which one they want. The problem I have is, the list only displays one specific field of data.

For instance, I have a table called Vehicle details, containing “Make, Model, age… etc”. however when I set up the function in the drop down box it only lets me select one specific column I.E Make or Model.

Is there a way i can retrieve a whole column from SQL server and display it as one list. I.E Option one is Ford, Focus, 5. The next option is Ford, Fiesta, 10.

Is it because this line of code is a single option? this.comboBox1.DisplayMember = “Make”; When i try to edit it in the GUI it basically tells me no.

Any help would be appreciated.

The idea is when a customer selects their option and presses a submit button it will fire the information over the sql server.




There is indeed only one DisplayMember, but that member can be anything you like. For example, if you’re binding the combo box to a list of objects, give the object a property that displays what you want:

public class MyClass
    public string Make { get; set; }
    public string Model { get; set; }

    public string Display { get { return $"{Make} - {Model}"; } }

Then you can bind to that property:

comboBox1.DisplayMember = "Display";

Similarly, if your results are something like a DataTable bound to the results of a SQL query, your query can also return a calculated result:

  Make + ' - ' + Model AS Display

Whatever you’re binding the control to, give it the calculated result that you’re looking for. The control itself is very generic, but you have specific control over your objects/data and can include in them all of the logic you like.

9 People found this is helpful