Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
4.00/5 (2 votes)
I have a combo box which is data bind to its “selected value” property and datasource is bind to a view in SQL database. It works fine, when I select and change value everything is updated correctly; however I have a button which should allow to user to add value to the lookup datasource.
My problem is that whenever user adds new item, the datasource is updated and in my lookup combobox the first value from the list is selected always, no matter if previously another value was selected in my combo.
Here is my button click event:
C#
private void addPersonBtn_Click(object sender, EventArgs e)
{

        EditPerson frmEditPerson = new EditPerson();
        if (frmEditPerson.ShowDialog() == System.Windows.Forms.DialogResult.OK)
        {
            ownerComboBox.BeginUpdate();
            this.viewLookupPersonTableAdapter.Fill(this.personDataSet.viewLookupPerson);
            ownerComboBox.EndUpdate();
        }
        frmEditPerson.Dispose();
}

Problem is that as soon as viewLookupPerson is filled, the value in my combo is changed to the first value in the list.
I tried also to store the value of selected item, like this:
C#
DataRowView rw = (DataRowView) ownerComboBox.SelectedItem;
ownerComboBox.BeginUpdate();
this.viewLookupPersonTableAdapter.Fill(this.personDataSet.viewLookupPerson);
ownerComboBox.EndUpdate();
ownerComboBox.SelectedItem = rw;

but still does not work… I know I could iterate through the combo box but there has to be an easier solution…
Posted

Hello

If you want to keep the previous item:

C#
//DataRowView rw = (DataRowView) ownerComboBox.SelectedItem;
object selectedItem = this.ownerComboBox.SelectedItem;
ownerComboBox.BeginUpdate();
this.viewLookupPersonTableAdapter.Fill(this.personDataSet.viewLookupPerson);
ownerComboBox.EndUpdate();
//ownerComboBox.SelectedItem = rw;
if (this.ownerComboBox.Items.Contains(selectedItem))
   this.ownerComboBox.SelectedItem = selectedItem;
 
Share this answer
 
v2
Comments
slkr 27-Feb-12 2:04am    
It still does refresh... this is similar to what I did with the row as DataRowView but it does not work.
Shahin Khorshidnia 27-Feb-12 4:28am    
Because the ComboBox is bound.

I don't know why you've done so.
slkr 27-Feb-12 4:50am    
I want to show data from the table: that is why the bind data controls are made for. In this case, my combo should lookup for a value from the different table; however I would like to allow user to add new lookup fields. I do not want to loop through the combo every time I need to refresh data...
I can do it like this:
C#
if (frmEditPerson.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
    Guid gd = ((MainDataSet.PersonRow)((DataRowView)mainBindingSource.Current).Row).owner;
    ownerComboBox.BeginUpdate();                    this.viewLookupPersonTableAdapter.Fill(this.personDataSet.viewLookupPerson);
    ownerComboBox.EndUpdate();
    ((MainDataSet.PersonRow)((DataRowView)mainBindingSource.Current).Row).owner = gd;
}


but it should not be that difficult... Here I actually set again the value directly to the field of the bind table "Person" (owner is a field in Person, of type Guid).
However, this way my row state might change without proper reason...
 
Share this answer
 
This is because even though you adding a new item to your lookup collection, you are still refreshing the data source. So it will ignore the currently selected item and show the first item again.

For a simple fix why don't you set your selected index to the index of the last item in your lookup collection after adding the new item. then it will show that item in the combobox as selected.

C#
comboBox1.SelectedIndex = index;
 
Share this answer
 
Comments
slkr 27-Feb-12 2:02am    
This is not a solution since there might be new items in the combo and the index of the previously selected item is now different.

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