hi,

Question 1

i am developing a project in C#.Net, and when a form loads data from the SQL client database is added to four combo boxes in the form, i need to change the data in the combo box when the check box is ticked. I wrote a code for this task but it give a value "System.Data.DataRowView" in the combo box.The code i wrote is added below.

code:

if (cBoxITAR.CheckState == CheckState.Checked)
            {
                db.openConnection();
                String query = @"Select  FirstName From Employee  Where  Citizenship = 'US' ";
                SqlCommand comm = new SqlCommand(query, DB.getConnection());
                SqlDataAdapter da = new SqlDataAdapter(comm);
                DataTable dt = new DataTable();

                da.Fill(dt);

                cmbPI.DataSource = dt;
                cmbPI.DisplayMember = "FirstName" + " " + "LastName";
                

                db.closeConnection();
            }
            else
            {
                LoadNames();
               

            }

what is the error in this. can someone pin point it for me???

Question 2

if the change were to be added to 4 combo boxes how do i need to change the code.so that when i click the check box the four combo boxes will be updated with new data and then the user can select four different values in those four combo boxes.
how can I change the code for this.

thank you!!

it give a value "System.Data.DataRowView" in the combo box.The code i wrote is added below.

cmbPI.DisplayMember = "FirstName" + " " + "LastName";

This is happening because there is no property of DataRowView called "FirstName LastName". It couldn't, because there's a space in there.

If you read the MSDN documentation for the DisplayMember property, you'll see that you have to specify the name of an actual property of the object you're putting in the combo box. The DisplayMember property doesn't actually do anything, it just tells the combo box where to get its value.

If there's a property of your DataRowView that will provide the text you want to show in the combo box, specify the name of that property here. If there isn't one, you can create your own class to wrap the DataRowView which provides a string-valued property that is formatted how you want, then put those objects in the combo box instead of using the DataRowView directly.

hi, please can u give more details, it is not very clear to me. I am not familiar with this language.
this code works, cmbPI.DisplayMember = "FirstName";
but how to i append another parameter to the combo box.


In the code i have quoted below the first line works but the second doesn't.Why is that??

cmbPI.DisplayMember = "Firstname";
cmbPI.ValueMember = "LastName";

thank you

this code works

cmbPI.DisplayMember = "FirstName"

but how to i append another parameter to the combo box.

In the code i have quoted below the first line works but the second doesn't.Why is that??

cmbPI.DisplayMember = "Firstname";
cmbPI.ValueMember = "LastName";

The MSDN documentation for DisplayMember and ValueMember have all the information you need.

You can put any kind of object you want in a ComboBox. The ComboBox doesn't know what the important properties are for your object, so you'll have to tell it. DisplayMember represents the name of a property to look for on objects in the ComboBox when displaying the list. These would work:

cmbPI.DisplayMember = "FirstName"
cmbPI.DisplayMember = "LastName"

This would not:

cmbPI.DisplayMember = "FirstName" + " " + "LastName"

...because there is no property named "FirstName LastName" on the objects you're putting in the ComboBox.

So—if the objects you're putting in the ComboBox have a single property that returns what you want to display, that's what you put in DisplayMember. ValueMember works in a similar way, but instead of indicating which property to use for displaying list items, it tells the ComboBox which property of your list objects to use for its SelectedValue property.

Read the MSDN documentation; follow links and read more documentation. It may be a little overwhelming at first, but it's the best way to learn exactly what is going on.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.