Вопрос

I am using Visual Studio 2012 and Access 2010 with table adapters. This is for a school project.

I am currently developing a system in C# that uses an Access database with the table CUSTOMER which has a one to many relationship to the table VEHICLE. This is so one customer can be associated with many vehicles.

When entering a vehicle into the system, there is a required combobox that shows the names of all the customers in the system to associate that vehicle to(a vehicle MUST be associate with a customer).

My problem is that the CUSTOMER table uses auto-increment, so when a customer is deleted, the customer ids are no longer necessarily sequential. The logic for my combobox assumes that the top customer name is customer id 1, the second is 2, and so on. This is causing inconsistency's in my application.

My idea to fix this problem was to load all the customer ids from the customer table into an array in order at run time and then using that array as a reference(obviously updating it with added and deleted customers). However, I'm not sure how to code stepping through the table to get the ids or even if this is the best solution. Any thoughts?

Это было полезно?

Решение 3

When you load your customers. Set the value field to the customerID and the text field to the customerName field

Другие советы

Don't EVER rely on record position within a resultset matching a record autoincrement number. Not only this is absolutely not reliable but it will likely present all sorts of challenges when it comes to UI filtering or ordering. In your scenario for exemple, you'd probably want your combobox sorted by names in alphabetical order. Therefore, relying on record index matching record autoincrement value will most definitely lead to bugs and data corruption.

Moving on to your actual problem now... Let's suppose your Customer class is like this:

class Customer
{
    public int Id {get; set;}
    public int Name {get; set;}
}

You can bind your combobox to a collection of Customer object and set two of its properties:

combobox.DisplayMember = "Name";
combobox.ValueMember = "Id";

DisplayMember property tells your control which property of your object it should use for display within the list, while ValueMember determines what field it uses for value. The latter is especially useful for databinding, which I believe you should use in your scenario.

Whenever you want to programmatically access the selected customer's Id value, simply check on combobox.SelectedValue property.

You can also use drew_w's technique, but be aware that while it works too, it will not be as easy to use in databinding, if possible at all.

One possible solution to adding data to a windows forms combo box is to make sure your object overrides "ToString". Then you can place the actual objects in the combobox list. Lets assume your object is defined like:

public Class Customer {
   public int CustomerId { get; set; }
   public string First { get; set; }
   public string Last { get; set; }

   // here is where you decide what to display in the combo box:
   public override string ToString() { return First + Last; }
}

Then you just add the items normally to the combo box:

// declare customers
Customer []myCustomers = null;

// load customers ... then show:
myCombobox.Items.AddRange(myCustomers);

Now when the user selects a customer in the selected index changed you can do the following to get the actual customer object:

var customerObject = (Customer)myCombobox.Items[myCombobox.SelectedIndex];

Now you don't have to worry about the order they are added to the combo box or indexes or anything. The objects store the id's from the database and everything works based on that! Best of luck!

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top