views:

121

answers:

2

I know how to connect to an access database and so on.

My question is, I want to select an item from a list box, and then it must search the access database for the item I selected and display all its contents in textboxes. For example:

In the listbox I have the following added:

Car 1
Car 2
Car 3
etc.

If I select Car 2, I want it to read the database and display all Car 2's properties in textboxes. So for example, once I select it, it can display Horsepower in a specific textbox, max speed in a specific text box, year model in a specific text box etc.

Could anyone help me out with this?

A: 

Assuming you have 1 textbox per property that will change whenever you select something different from your listbox here is what I would do. It sounds like you are already populating your listbox with some category from your database, possibly CarType. I would use LINQ to run a query to populate the results within the given textbox.

var query = from record in myTable.AsEnumerable()
            where record.CarType == myListBox.SelectedValue
            select record;

foreach (var record in query)
{
    horsePower.Text = record.HorsePower;
    //and so on
}

It sounds like you are setting up a custom control for this. However, I would suggest using a Data Grid to do what you are trying to do, instead of Text Boxes.

jsmith
A: 

Have you tried using data binding?

listBox.DataSource = dataTableCars;
listBox.DisplayMember = "car_name";
listBox.ValueMember = "car_id";

txtBox.DataBindings.Add("Text",dataTableCars,"car_name");
txtBox.DataBindings.Add("Text",dataTableCars,"car_driver_name");

etc.

dmitko