Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > Combobox items deleted after updating datasource
 

Combobox items deleted after updating datasource

Hi,

I set a combobox datasource to a simple data table. The displayMember is set to the description column, and the valueMember to the ID column, and all is well.

But when I add a row to the Table, the combo items are suddenly deleted !

I can see that the table is updated, but there are no items in the combobox. Assigning the dataSource property to the data table again, doesn't help.

Any idea what's happening?

Thanks,

Geshem  Monday, March 12, 2007 7:31 AM
are you able to post the code you are using? Are you also using ASP.NET or a Winforms application?
ahmedilyas  Monday, March 12, 2007 9:48 AM

Hi,

I tried a sample , having a combo(datasource istable(ID,name)and a form on it.And the button handler of the form is as follows

private void button1_Click(object sender, EventArgs e)

{

this.gopiTestDBDataSet.TestEmp.AddTestEmpRow(index, "name"+index.ToString());

index++;

}

I found everything was going fine.No items were deleted,infact getting added whenever i click on the button.Please check ur code once.If the problem is not solved still, posting code will help more in fixing this issue.

Thanx,

Ch.T.Gopi Kumar.

TilakGopi  Monday, March 12, 2007 9:57 AM

I can post part of the code, i hope it will help.

the data binding part :

cmbUserPos.DataSource = frmEntry.TheDataBase.UserPosition;

cmbUserPos.DisplayMember = "Desc";

cmbUserPos.ValueMember = "ID";

cmbUserPos.DataBindings.Add("SelectedValue", bSourceUsers, "PositionID");

*****

The part of adding a row:

Im getting the new data from an "inpout box", then creating a new row and adding it. in the debugger i can see that the combo is cleared when the row is added

private void btnAddUserPosition_Click(object sender, EventArgs e)

{

//Getting new data from input box:

InputDialog Pos = new InputDialog("New Position", "Enter Position Description");

Pos.ShowDialog();

string Desc = Pos.GetInput;

Pos.Close();

if (Desc != "")

{

//Adding new row:

DataRow positionRow = frmEntry.TheDataBase.UserPosition.NewRow();

positionRow["Desc"] = Desc;

ComboInputDialog Secure = new ComboInputDialog("Please Select Security Level For "+Desc,"Position Security Level",cLNLDatabase.Security, "Desc", "ID");

Secure.ShowDialog();

int SecurityID = (int)Secure.GetValue;

if (SecurityID >= 0)

{

positionRow["SecurityID"] = SecurityID;

}

else

{

positionRow["SecurityID"] = 0; //NONE

}

frmEntry.TheDataBase.UserPosition.Rows.Add(positionRow); //At this point the combo items are deleted !

}

Geshem  Monday, March 12, 2007 10:10 AM

I played with it for a while, and came to the conclusion that binding of the SelectedValue to the binding source is what causing this problem. I don't know why.

My solution was not very elegant- delete the binding before adding new row, then adding the binding back.

If someone have an explanation, and/or a better solution, I would love to hear it.

Geshem  Thursday, March 15, 2007 1:21 PM

You can use google to search for other answers

Custom Search

More Threads

• datagridview vertical scroll bar
• Changing Control Properties not being updated in data object
• Datagrid can´t load scrollbar after get data from webservice using Thread at Form.Shown
• Problems with Data Adapter Wizard
• User Control Binding
• error
• Extending Binding Navigator
• Send Columns to TablurReport
• Databinding using typed datasets
• How to get the value of checkbox in the DataGridView?