Jack Johnstone
Jack Johnstone

Reputation: 1535

Two datagridviews in one windows form = not possible to save data to the second datagridview

  1. I create a winform
  2. I´m adding a datagridview by dragging a table from data sources
  3. the result: I´ve got a winform with a datagridview and a bindingnavigator
  4. I´m adding a second datagridview to the same winform by dragging another table from data sources
  5. the result: same as bullet 3, but now with two datagridviews; a second bindingnavigator is not added though, don´t ask me why!
  6. I copy the existing bindingnavigator and a adapts the copy to the second datagridview
  7. I look into the .Designer.cs file, and it looks perfect regarding consistency
  8. Now to the problem - even though both datagridviews have their own BindingNavigatorSaveItem_Click method, with code pointing out their respective bindingsources... it´s not possible to save changes from the second datagridview to the database!

    Anyone seen this? Any suggestions what to do?
    

Upvotes: 2

Views: 2098

Answers (3)

MirrorOnTheWall
MirrorOnTheWall

Reputation: 1

So disclaimer, I am new at this but hopefully it helps someone else who is enjoying the journey of trying new things.

Usually when you follow steps 1 to 3 as John Johnstone did you get the DatabaseDataSet, tablenameBindingSource, tablenameTabledapter, tableAdapterManager and the tablenameBindingNavigator. If you add another datagridview you will notice that they are missing for the second datagridview.

  1. Add datagridview for both tables in Form1.

  2. Add another form(Form2).

  3. Add the datagridview with the missing controls on Form2. You will notice it has everything at the bottom of the designer window(grey area).

  4. Copy and paste what is missing from this form(Form2) to Form1 from the bottom of the designer window(grey area) and undock the control(on the Designer Form) with all the buttons("+" ,"X" save btn, etc) for the datagridview in Form2 copy it and paste it in Form1. You will have to rename tableAdapterManager to tableAdapterManager1. If you don't the table will only update when you click the "+" button

  5. Copy all the code in the methods that where in Form2 and put them in methods for Form1(See code below). It should work now or at least it worked for me. I was using Visual Studio 2019 with a localDB. If it does not keep exploring you will get there eventually and learn a lot along the way.

     private void customersBindingNavigatorSaveItem_Click(object sender, EventArgs e)
     {
         this.Validate();
         this.customersBindingSource.EndEdit();
         this.tableAdapterManager.UpdateAll(this.tempDatabaseDataSet);
    
     }
     private void inventoryBindingNavigatorSaveItem_Click(object sender, EventArgs e)
     {
         this.Validate();
         this.inventoryBindingSource.EndEdit();
         this.tableAdapterManager1.UpdateAll(this.tempDatabaseDataSet);
     }
    
     private void Form1_Load(object sender, EventArgs e)
     {   this.inventoryTableAdapter.Fill(this.tempDatabaseDataSet.Inventory);
         this.customersTableAdapter.Fill(this.tempDatabaseDataSet.Customers);
    
     }
    

Upvotes: 0

Jack Johnstone
Jack Johnstone

Reputation: 1535

I solved it like this (I thank mr/mrs/miss dretzlaff17 who started up some processes in what´s left of my doped brain)

  private void tableMeLikeBindingNavigatorSaveItem_Click(object sender, EventArgs e)
  {
     try
     {
        this.Validate();
        this.tableMeLikeBindingSource.EndEdit();

        // IMPORTANT: the following predefined generic Update command
        // does NOT work (sometimes)
        // this.tableAdapterManager.UpdateAll(this.rESOURCE_DB_1DataSet);

        // instead we explicitely points out the right table adapter and updates
        // only the table of interest...
        this.tableMeLikeTableAdapter.Update(this.rESOURCE_DB_1DataSet.TableMeLike);
     }

     catch (Exception ex)
     {
        myExceptionHandler.HandleExceptions(ex);
     }
  }

Upvotes: 2

dretzlaff17
dretzlaff17

Reputation: 1719

I would suggest to set the DataSource property of the DataGridView in code through the use of a DataTable or object collection instead of using a bindingnavigator.

Upvotes: 0

Related Questions