delete a record in the grid AND database

4 posts, 1 answers
  1. Joe
    Joe avatar
    26 posts
    Member since:
    Feb 2012

    Posted 20 Apr 2015 Link to this post

    I have a databound grid.

    When I right click on a row and choose "delete row" I want it to delete it in the database as well.

    My code seems pretty simple:

    private void radGridView1_RowsChanging(object sender, GridViewCollectionChangingEventArgs e)
          {
     
              if (e.Action == Telerik.WinControls.Data.NotifyCollectionChangedAction.Remove)
              {
                            
                  DialogResult dr = MessageBox.Show("Are you sure?",
                      "Delete Row", MessageBoxButtons.YesNo);
                  if (dr == DialogResult.Yes)
                  {
                      e.Cancel = false;
                      this.radGridView1.EndUpdate();
                      this.rep_assigned_stop_matrixTableAdapter.Update(first_choice_mainDataSet);
                  }
                  else
                  {
                      e.Cancel = true;
                  }
                   
              }
     
          }

     

    It steps through it fine, the row is gone but the database does not get updated UNLESS I edit another field, then it calls this and it not only does the field edit but it also removes the row:

     

    private void radGridView1_CellValueChanged(object sender, GridViewCellEventArgs e)
            {
     
                IEditableObject editbaleObject = e.Row.DataBoundItem as IEditableObject;
                DataRowView dataRowView = e.Row.DataBoundItem as DataRowView;
               
                  if (editbaleObject != null)
                {
                    editbaleObject.EndEdit();
                }
                if (dataRowView != null)
                {
                    var test = dataRowView.DataView.Table;
                    string whatiswas = test.ToString();
                    string curTable = dataRowView.DataView.Table.ToString();
     
                    if (dataRowView != null && this.allowEdits.Checked)
                    {
                        if (curTable == "rep_info")
                        {
                            //this.rep_infoTableAdapter.Update(first_choice_mainDataSet.rep_info);
                            this.rep_infoTableAdapter.Update(dataRowView.Row);
                        }
                        else if (curTable == "rep_assigned_stop_matrix")
                        {
                            //this.rep_assigned_stop_matrixTableAdapter.Update(dataRowView.Row);
                            this.rep_assigned_stop_matrixTableAdapter.Update(first_choice_mainDataSet);
                        }
                    }

  2. Answer
    Dimitar
    Admin
    Dimitar avatar
    1415 posts

    Posted 21 Apr 2015 Link to this post

    Hello Joe,

    Thank you for contacting us.

    In the RowsChanging event the changes are still not finished and that is why the table is not updated. You should use the RowsChanged event to update the database. This event will fire only if the rows changing is not canceled which means that you can keep your existing code:
    void radGridView1_RowsChanging(object sender, Telerik.WinControls.UI.GridViewCollectionChangingEventArgs e)
    {
        if (e.Action == Telerik.WinControls.Data.NotifyCollectionChangedAction.Remove)
        {
            DialogResult dr = MessageBox.Show("Are you sure?",
                "Delete Row", MessageBoxButtons.YesNo);
            if (dr == DialogResult.Yes)
            {
                e.Cancel = false;
     
            }
            else
            {
                e.Cancel = true;
            }
        }
    }
    void radGridView1_RowsChanged(object sender, Telerik.WinControls.UI.GridViewCollectionChangedEventArgs e)
    {
        northwindDataSet.CategoriesDataTable changes = northwindDataSet.Categories.GetChanges() as northwindDataSet.CategoriesDataTable;
     
        if (changes != null)
        {
            categoriesTableAdapter.Update(changes);
        }
    }

    Please let me know if there is something else I can help you with.

    Regards,
    Dimitar
    Telerik
     

    See What's Next in App Development. Register for TelerikNEXT.

     
  3. UI for WinForms is Visual Studio 2017 Ready
  4. Joe
    Joe avatar
    26 posts
    Member since:
    Feb 2012

    Posted 21 Apr 2015 in reply to Dimitar Link to this post

    thank you,

    This mostly worked for me.  For some reason however, at times the tableAdapter seemed to not realize there were changes.

    This happened when the following occurred:

    1. Create a record - verify that it saved to the database

    2. delete the record - it would not delete it.

    If you left the grid, reloaded it and then deleted it all was fine.

    I wound up just adding a button and grabbing info from the current row and calling a removeRow routine.

    Thank you,

     Joe

  5. Dimitar
    Admin
    Dimitar avatar
    1415 posts

    Posted 23 Apr 2015 Link to this post

    Hi Joe,

    Thank you for contacting us.

    You should call the AcceptChanges method after the update and then refill the table:
    void radGridView1_RowsChanged(object sender, Telerik.WinControls.UI.GridViewCollectionChangedEventArgs e)
    {
        if (e.Action != Telerik.WinControls.Data.NotifyCollectionChangedAction.Reset)
        {
            northwindDataSet.CategoriesDataTable changes = northwindDataSet.Categories.GetChanges() as northwindDataSet.CategoriesDataTable;
     
            if (changes != null)
            {
                categoriesTableAdapter.Update(changes);
                northwindDataSet.Categories.AcceptChanges();
              
                this.categoriesTableAdapter.Fill(this.northwindDataSet.Categories);
            }
        }
         
    }

    Please let me know if there is something else I can help you with. 
     
    Regards,
    Dimitar
    Telerik
     

    See What's Next in App Development. Register for TelerikNEXT.

     
Back to Top