Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > Problems using DataSource, DisplayMember and ValueMember with ComboBox
 

Problems using DataSource, DisplayMember and ValueMember with ComboBox

I have a byte field in my database bound to a ComboBox.
I want to display a descriptive text in the ComboBox rather than the byte.

I have created class called "UserLevel", populated it and then linked it to the ComboBox.

        public class UserLevel
        {
            private byte myIDNo;
            private string myDesc;

            public UserLevel(string strDesc, byte strIDNo)
            {

                this.myIDNo = strIDNo;
                this.myDesc = strDesc;
            }

            public byte UserIDNo
            {
                get
                {
                    return myIDNo;
                }
            }

            public string UserDesc
            {

                get
                {
                    return myDesc;
                }
            }

            public override string ToString()
            {
                return this.UserIDNo.ToString() + " - " + this.UserDesc;
            }
        }


        private void UsersForm_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'support_LogDataSet.Users' table. You can move, or remove it, as needed.
            // Populates the list box using DataSource. 
            // DisplayMember is used to display just the long name of each state.
            ArrayList UserLevels = new ArrayList();
            UserLevels.Add(new UserLevel("Read Only (0)", 0));
            UserLevels.Add(new UserLevel("Normal (1)", 1));
            UserLevels.Add(new UserLevel("Administrator (9)", 9));
            UserLevels.Add(new UserLevel("6", 6));
            UserLevels.Add(new UserLevel("8", 4));

            userLevelComboBox.DataSource = UserLevels;
            userLevelComboBox.DisplayMember = "UserDesc";
            userLevelComboBox.ValueMember = "UserIDNo";


            this.usersTableAdapter.Fill(this.support_LogDataSet.Users);

        }



The ComboBox displays the "UserDesc" displays the "Ready Only (0)" in the drop down box, butit will not store the corresponding value(e.g. 0) in the database - it does not throw any errors either!
The selected Value is set to the numeric value.
If I select "6" or "8" from the drop down, then the database gets updated with 6 or 4 (not 8!).

What am I doing wrong?
Phil Gameson  Monday, September 21, 2009 10:54 AM
In that case what you did would be the right thing. The databinding would be done automatically to the default property, however we can make changes to it. There is no harm in doing this.

Regards,
Manoj Mishra
  • Marked As Answer byPhil Gameson Monday, September 21, 2009 5:26 PM
  •  
manoj_mishra_18  Monday, September 21, 2009 5:13 PM
Hi Phil,

Please let me know if you get "0 - Read Only (0)" in the database when you select "Read Only (0)". Try saving it to a string field in the database to check this.

And as far as the following issue:-
"If I select "6" or "8" from the drop down, then the database gets updated with 6 or 4 (not 8!)."

I see that you have declared 4 in the following code as the value.

"UserLevels.Add(new UserLevel("8", 4));"


hence I guess that you are getting 4 instead of 8. Try using the following code

"UserLevels.Add(new UserLevel("8", 8));"


Regards,
Manoj
manoj_mishra_18  Monday, September 21, 2009 1:45 PM
Hi Manoj,

I tried binding a similar ComboBox to a string field, and amended my Class to use string fields rather than one string and one byte fields.

I can confirm that whatever appears in the drop down box is what appears in the database.
The "UserLevels.Add(new UserLevel("8", 4));" code was simply to check what would happen!

So the problem seems to be that although the ComboBox sets the Value (as shown by userLevelComboBox.SelectedValue.ToString()) to "4" rather than "8", it is 8 that ends up in the database.

I have found away around this...

In the xxx.Designer.cs file the following binding appeared:

            this.fullNameComboBox.DataBindings.Add(new System.Windows.Forms.Binding("Text", this.usersBindingSource, "FullName", true));
I manually changed this to:

            this.fullNameComboBox.DataBindings.Add(new System.Windows.Forms.Binding("SelectedValue", this.usersBindingSource, "FullName", true));  // Manually changed "Text" to "Value"
This has done the trick, but I don't as a rule make any changes in the ...Designer.cs files. Is this the right way of going about things?
Phil Gameson  Monday, September 21, 2009 2:21 PM
Phil,

To understand you issue better, please answer the following question:-

You have declared

UserLevels.Add(new UserLevel("Read Only (0)", 0));
UserLevels.Add(new UserLevel("Normal (1)", 1));
UserLevels.Add(new UserLevel("Administrator (9)", 9));
UserLevels.Add(new UserLevel("6", 6));
UserLevels.Add(new UserLevel("8", 4));

Do you want

"Read Only (0)"
"Normal (1)"
"Administrator (9)"
"6"
"8"

to be stored in the database or do you want

0
1
9
6
4

to be added to the database based on the corresponding selection in the combobox?

Regards,
Manoj Mishra
manoj_mishra_18  Monday, September 21, 2009 3:02 PM
Manoj,


I want

"Read Only (0)"
"Normal (1)"
"Administrator (9)"
"6"
"8"
Phil



Phil Gameson  Monday, September 21, 2009 4:45 PM
In that case what you did would be the right thing. The databinding would be done automatically to the default property, however we can make changes to it. There is no harm in doing this.

Regards,
Manoj Mishra
  • Marked As Answer byPhil Gameson Monday, September 21, 2009 5:26 PM
  •  
manoj_mishra_18  Monday, September 21, 2009 5:13 PM

You can use google to search for other answers

Custom Search

More Threads

• Simple Display of Values in a 2-D Array
• where to open my connection object?
• non-bound grid control in .NET? (like MSHFlexGrid)
• Passing Parameter to Access Query
• Binding to tables with a many-many relation
• datagridview combobox databinding problem
• Need help converting KB318607 into Virtual DataGrid...
• Change table orientation in DataGridView
• How to assign a null value for foreign key in DataTable.AddRow()
• Set BindingSource.DataSource Odd Behaviour