none
Need to update combo box when add, modify, or delete records to db.

    Question

  • I have a combo box that is bound to a table in a dataset.  When I add, modify or delete a record in the  dataset, i need that to be reflected in the combo box.  I use the generated functions of the dataset.  The database itself changes accordingly but not the bound control.  I am using Visual C++ in VS 2005.  All the databinding was done with drag and drop in the visual designer.

    Thanks in advance for your help

    this is the code that i am using.  most of it was generated when i built the form.

    private: System::Windows::Forms::BindingSource^  companiesBindingSource1;
    
    this->companiesBindingSource1 = (gcnew System::Windows::Forms::BindingSource(this->components));
    (cli::safe_cast<System::ComponentModel::ISupportInitialize^  >(this->companiesBindingSource1))->BeginInit();
    
    //sets up binding source
    this->companiesBindingSource1->DataMember = L"Companies";
    this->companiesBindingSource1->DataSource = this->CallSheetDataSet;
    
    
    //binds here
    this->deleteRec2->DataSource = this->companiesBindingSource1;
    this->deleteRec2->DisplayMember = L"Name";
    this->deleteRec2->FormattingEnabled = true;
    this->deleteRec2->Location = System::Drawing::Point(115, 52);
    this->deleteRec2->Name = L"deleteRec2";
    this->deleteRec2->Size = System::Drawing::Size(197, 21);
    this->deleteRec2->TabIndex = 11;
    this->deleteRec2->SelectedIndexChanged += gcnew System::EventHandler(this, &Form1::comboBox10_SelectedIndexChanged);
    
    (cli::safe_cast<System::ComponentModel::ISupportInitialize^  >(this->companiesBindingSource1))->EndInit();
    
    //on button press deletes item selected in combobox from database
    //Need to refresh data in combobox once delete is complete
    private: System::Void deleteButton_Click(System::Object^  sender, System::EventArgs^  e) {
                     System::String^ deleteType = deleteRec1->GetItemText(deleteRec1->Items[deleteRec1->SelectedIndex]);
    
                     if(deleteType == "Company"){
                         this->CompaniesTableAdapter->Delete(deleteBox1->Text);
    //end edit doesnt work here		     
    		     this->companiesBindingSource1->EndEdit();
    		     this->deleteRec2->DataSource = this->companiesBindingSource1;
    //tried forcing refresh here, doesnt work
    		     this->companiesBindingSource->ResetBindings(false);
    		     this->companiesBindingSource1->ResetBindings(false);
                     }//if company

    Thursday, January 16, 2014 2:04 AM

Answers

  • I have solved my problem.  By using an extra layer of error checking to make sure that there are elements in the comboBox, i used the Fill(<DataTable>) method of the tableAdapter class.  This refreshed the table that the combobox is attached to. 

    Thanks for helping

    Monday, January 20, 2014 4:01 PM

All replies

  • I have a combo box that is bound to a table in a dataset.  When I add, modify or delete a record in the  dataset, i need that to be reflected in the combo box.  I use the generated functions of the dataset.  The database itself changes accordingly but not the bound control.  I am using Visual C++ in VS 2005.  All the databinding was done with drag and drop in the visual designer.

    Thanks in advance for your help

    Friday, January 10, 2014 8:22 PM
  • Can you show us the code that was generated to databind the Combo?

    ~~Bonnie DeWitt [C# MVP]

    http://geek-goddess-bonnie.blogspot.com

    Monday, January 13, 2014 5:41 AM
  • Hi,

    Based on my understanding, you want to know when the dataset update, you should create new object which implement INotifyPropertyChanged interface notifies clients that a property value has changed. You could re-bind or refresh data when you change data and get notification.

    >>I am using Visual C++ in VS 2005. 

    In addition, I suggest you to post your thread in Visual C++ forum where you can contact VC++ experts

    Regards,


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Monday, January 13, 2014 5:55 AM
  • here is the code as per your request.

    private: System::Windows::Forms::BindingSource^  companiesBindingSource1;
    
    this->companiesBindingSource1 = (gcnew System::Windows::Forms::BindingSource(this->components));
    (cli::safe_cast<System::ComponentModel::ISupportInitialize^  >(this->companiesBindingSource1))->BeginInit();
    
    //sets up binding source
    this->companiesBindingSource1->DataMember = L"Companies";
    this->companiesBindingSource1->DataSource = this->CallSheetDataSet;
    
    
    //binds here
    this->deleteRec2->DataSource = this->companiesBindingSource1;
    this->deleteRec2->DisplayMember = L"Name";
    this->deleteRec2->FormattingEnabled = true;
    this->deleteRec2->Location = System::Drawing::Point(115, 52);
    this->deleteRec2->Name = L"deleteRec2";
    this->deleteRec2->Size = System::Drawing::Size(197, 21);
    this->deleteRec2->TabIndex = 11;
    this->deleteRec2->SelectedIndexChanged += gcnew System::EventHandler(this, &Form1::comboBox10_SelectedIndexChanged);
    
    (cli::safe_cast<System::ComponentModel::ISupportInitialize^  >(this->companiesBindingSource1))->EndInit();
    
    //on button press deletes item selected in combobox from database
    //Need to refresh data in combobox once delete is complete
    private: System::Void deleteButton_Click(System::Object^  sender, System::EventArgs^  e) {
                     System::String^ deleteType = deleteRec1->GetItemText(deleteRec1->Items[deleteRec1->SelectedIndex]);
    
                     if(deleteType == "Company"){
                         this->CompaniesTableAdapter->Delete(deleteBox1->Text);
    //end edit doesnt work here		     
    		     this->companiesBindingSource1->EndEdit();
    		     this->deleteRec2->DataSource = this->companiesBindingSource1;
    //tried forcing refresh here, doesnt work
    		     this->companiesBindingSource->ResetBindings(false);
    		     this->companiesBindingSource1->ResetBindings(false);
                     }//if company
    }

    Tuesday, January 14, 2014 9:51 PM
  • I'm sorry, I didn't notice initially when I asked you to post some code that you were talking about C++, not C#. As Marvin suggested earlier, you should post your question in the C++ forum, but I think a better link would be here: http://social.msdn.microsoft.com/Forums/en-US/home?forum=vcgeneral

    ~~Bonnie DeWitt [C# MVP]

    http://geek-goddess-bonnie.blogspot.com


    • Edited by BonnieBMVP Wednesday, January 15, 2014 6:08 AM Edit URL link
    Wednesday, January 15, 2014 6:00 AM
  • Hello,

    >>I have a combo box that is bound to a table in a dataset.  When I add, modify or delete a record in the  dataset, i need that to be reflected in the combo box.  I use the generated functions of the dataset.  The database itself changes accordingly but not the bound control.

    Have you tried to debug this application with breakpoints?

    What was the result in the output window?

    Did you find how the parameters changed ?

    Could the combo box receive the changes of database ?

    Furthermore, you can upload sample code on https://skydrive.live.com/ and then share the link here so that we could test the code locally.

    Best Regards,

    Jane.

     


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Friday, January 17, 2014 2:06 AM
  • I have solved my problem.  By using an extra layer of error checking to make sure that there are elements in the comboBox, i used the Fill(<DataTable>) method of the tableAdapter class.  This refreshed the table that the combobox is attached to. 

    Thanks for helping

    Monday, January 20, 2014 4:01 PM