Click here to Skip to main content
16,017,238 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello,

In my application i have a combobox populated from database:

C#
Program.Connection.CommandText = "SELECT LastName + ', ' + FirstName + ' ' + IIF(MiddleName, MiddleName,'') AS NumeComplet, ClientId FROM Clients GROUP BY ClientId, LastName, FirstName, MiddleName ORDER BY ClientId";
            DataTable Table = new DataTable();
            Program.Connection.FillDataTable(Table, true);
            cboNumeClient.DataSource = Table;
            cboNumeClient.DisplayMember = "NumeComplet";
            cboNumeClient.ValueMember = "ClientId";
            cboNumeClient.Focus();
            NumeClient();
            ClientiAdaugati = true;
            InformatiiClient();


and a selected index event:

C#
private void cboNumeClient_SelectedIndexChanged(object sender, EventArgs e)
       {
           if (this.ClientiAdaugati)
           {
               NumeClient();
               InformatiiClient();
           }
       }



In the database i have customers with the same first name and last name, each one have it's own ID.

When i select i client i use the ID to populate a datagridview.

The problem is:

For example the:
client John John - ID - 2345, and
client John John - ID - 2390.

When i select the second name (id - 2390) from combobox, my datagridview fill whit according data, but when i click on a control from my windows, my combobox auto select the firs client (id 2345).

How do i resolve that?
Posted

1 solution

To start with, there is no such event, cboNumeClient_SelectedIndexChanged. It can be you handler which is added to an invocation list of the event instance SelectedIndexChanged of some combo box, but this is not apparent from your code. You also should show where the handle is added via the '+=' operator. As you did not complain that nothing happens on combo box selection, I assume the handler is added properly.

Now, no matter what discrepancy in ID you have, it is not apparent that your handler code does right thing, at least, it is not written in a methodically right way. You should do something like this:
C#
CboNumeClientSelectedIndexChangedHandler(object sender, EventArgs e) {
    ComboBox comboBox = (ComboBox)sender;
    // extract current selected index from combo box
    // or extract ID or any other information from selected item
    // use it
    // ...
}


Note that I also renamed you handler, to avoid the violation of Microsoft naming conventions always caused by that auto-generated code. Yes, you are supposed to rename all auto-generated names, but the reason I just explained, and also to give everything semantic names.

Now, even that is not a best approach. When you use a combo box, its list elements could be any objects. I think you are using just strings. This is bad, because then you have to extract some data from a string, which is at least bad for maintenance, and can be unreliable. The civilized approach is this: you create some data type, class or struct with all information you really need for your functionality. The only problem will be: what string will be shown in UI for each element? The answer is: whatever ToString returns. So, you also need to override ToString for your struct/class representing a combo box list item. Same technique can be used for list boxes, etc.

—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900