This is a migrated thread and some comments may be shown as answers.

delete a record in the grid AND database

3 Answers 359 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Joe
Top achievements
Rank 2
Joe asked on 21 Apr 2015, 03:16 AM

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);
                    }
                }

3 Answers, 1 is accepted

Sort by
0
Accepted
Dimitar
Telerik team
answered on 21 Apr 2015, 01:48 PM
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.

 
0
Joe
Top achievements
Rank 2
answered on 21 Apr 2015, 08:54 PM

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

0
Dimitar
Telerik team
answered on 23 Apr 2015, 12:57 PM
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.

 
Tags
GridView
Asked by
Joe
Top achievements
Rank 2
Answers by
Dimitar
Telerik team
Joe
Top achievements
Rank 2
Share this question
or