hi,
im designing a form that updates a customer details. when the user enter an id, the form displays all the fields of that customer. my problem is that i have a combobox that displays the status of the customer, i filled the combobox with four items and when the user choose a customer, it displayed the correct value from the combobox. but when the user doesn't choose a value from the combobox, the comboxbox gets null instead of the value it was assign with. i want that if the user doesnt change the value in the combobox, the combobox will automatically gets the value it was assign with.

I had a similar problem with a databound combo box and didnt figure it out but try comboBox.SelectedItem or comboBox.Text thats how i get the currently selected item or the text of a combobox

i tried that, and it didnt work...

I had a similar problem with a databound combo box and didnt figure it out but try comboBox.SelectedItem or comboBox.Text thats how i get the currently selected item or the text of a combobox

thanks for the replay. your suggestion will work if u selected that value from the combobox. in my case, however,the value is displayed without selecting it from the combobox

just after you populate the comboBox, call this property:

comboBox1.SelectedIndex = -1;

This way the by default will nothing be selected.
But this will only work if you populate comboBox manually; that means that the comboBox is not data bound.

I didnt really get you what do you want.
If I understand you correctly, when you get your data from the "some where" (DB), you populate comboBox and do the automatic selection. And your problem is that when you do it, there nothing gets selected. Am I right?

BTW: would be in a much help, if you would show us some code, instead of just talking here.
thx in advance,

just after you populate the comboBox, call this property:

comboBox1.SelectedIndex = -1;

This way the by default will nothing be selected.
But this will only work if you populate comboBox manually; that means that the comboBox is not data bound.

I didnt really get you what do you want.
If I understand you correctly, when you get your data from the "some where" (DB), you populate comboBox and do the automatic selection. And your problem is that when you do it, there nothing gets selected. Am I right?

BTW: would be in a much help, if you would show us some code, instead of just talking here.
thx in advance,

private void FrmUpdateCustomer_Load(object sender, EventArgs e)
{
// TODO: This line of code loads data into the 'finalProjectDataSet.TblCustomer' table. You can move, or remove it, as needed.
this.tblCustomerTableAdapter.Fill(this.finalProjectDataSet.TblCustomer);


}

private void textBox1_TextChanged(object sender, EventArgs e)
{

}

private void button1_Click(object sender, EventArgs e)
{
string s = comboBox1.SelectedValue.ToString();
// comboBox2.SelectedIndex = 0;
MessageBox.Show(comboBox2.SelectedText.ToString());

string x2 = textBox3.Text;
int numOfVisit = 0;
numOfVisit = int.Parse(x2);
try
{
Sys.sys1.updateCustomer(s, textBox1.Text, maskedTextBox1.Text, textBox2.Text, comboBox2.SelectedItem.ToString(), comboBox3.SelectedItem.ToString(), numOfVisit);
}
catch (NullReferenceException nu)
{
MessageBox.Show("יש ללחוץ על סטאטוס הזמנה ואיך שמעת");
MessageBox.Show(nu.Message.ToString());
}

}

thats my code. i bounded my primary key field(ID) to the DB and when the user select the id, it shows all of its other fields(age,birthday,status). the status field is a combobox that i populated it is not bound.when i select the value from the combobox it works just fine. the problem accures when i dont want to update the the status field, and leave the value just as it was. when the user doesnt select this field, although it shows the field value, it assign it null

Would you show in this code where is the problem? From that Icannot see what it could be.

You ae constantly chnaging methods on comboBox. Ones you select SelectedValue, next time you select SelectedText, and another time you select SelectedItem method.
Do you know the difference between them?
I bet you do not know, thats why you have problems.

Let me explain in general:
1. Combobox can have 2 different type of data in one row (item and value).
If you have a custom class:

public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            List<Person> list = new List<Person>();
            list.Add(new Person { ID = 1, Name = "Some name" });
            list.Add(new Person { ID = 2, Name = "Other one" });

            //now you can assing item and a value to comboBox (and before you have to bind the list to it):
            comboBox1.DataSource = list;
            comboBox1.DisplayMember = "Name";
            comboBox1.ValueMember = "Id";
        }

       //Ok, lets move on...
       //When you want to get the item and the value (Name and ID) on comboBox selection you can do:

        private void button1_Click(object sender, EventArgs e)
        {
            int _id = int.Parse(comboBox1.SelectedValue.ToString());
            string _name1 = comboBox1.Text;
            //or with a Person`s class help:
            string _name2 = (comboBox1.SelectedItem as Person).Name;

            MessageBox.Show(String.Format("Selected person is {0}, with ID {1}.", _name2, _id));
        }
    }

    class Person
    {
        public int ID { get; set; }
        public string Name { get; set; }
    }

Do you see now what is the Value and what is the Item??

But in your case, when you do not use DataSouce property, would mean - test the code by your self

public Form1()
        {
            InitializeComponent();
            comboBox1.Items.AddRange(new string[] { "a", "b", "c" });
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //string value = comboBox1.SelectedValue.ToString();  // HERE WIL BE A N ERROR, so this you can EXCLUDE!!!
            string item = comboBox1.SelectedItem.ToString();
            string selectedText = comboBox1.SelectedText;
            string text = comboBox1.Text;
            MessageBox.Show("SelectedItem: " + item + ", SelectedText: " + selectedText + " and Text: " + text);
        }

I hope I did some explanation, and you have to test both exampe (if I did it for you, you can test them out to learn).

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.