Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
Hello,
 
In my application i have a combobox populated from database:
 
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:
 
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 31-Jan-13 5:28am

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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:
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
  Permalink  

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

  Print Answers RSS
0 CHill60 205
1 Sergey Alexandrovich Kryukov 175
2 Zoltán Zörgő 175
3 Richard MacCutchan 175
4 Richard Deeming 170
0 Sergey Alexandrovich Kryukov 9,328
1 OriginalGriff 6,806
2 Peter Leow 4,442
3 Zoltán Zörgő 3,984
4 Richard MacCutchan 2,807


Advertise | Privacy | Mobile
Web03 | 2.8.150129.1 | Last Updated 31 Jan 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100