none
UpdateCommand - Doesn't work for one row. RRS feed

  • Question

  • Hi

    I have a GridView with 4 data columns and a Button Column.

    The form also has a "Save" button which has the following attached to it

            private void SaveChangesButton_Click(object sender, EventArgs e)
            {
                this.manufacturerMapBindingSource.EndEdit();
                try
                {
                    if (this.bizTalkAXDataSet.GetChanges() != null)
                    {
                        this.manufacturerMapTableAdapter.Update(this.bizTalkAXDataSet);
                    }
                }
                catch (Exception exception)
                {
                    MessageBox.Show(exception.Message);
                }
            }

    This works - No problem - it correctly updates any changed records in the gridview (It appears to know which records have changed and only update those - very clever).

    method()? which extracts the current row and then tries to get just that row updated.

    But what I want to do is to perform an update of a single record from the Button Column. So I have a "CellContentClick" method (?) which extracts the current row and then tries to updated it.

            private void manufacturerMapDataGridView_CellContentClick(object sender, DataGridViewCellEventArgs e)
            {
                if ( e.ColumnIndex == 5 ) // My Button Row
                {
                    DataRow rowToUpdate = (manufacturerMapDataGridView.CurrentRow.DataBoundItem as DataRowView).Row;
                    this.manufacturerMapTableAdapter.Update(rowToUpdate);
                }
            }
    But the update never works. I've checked (with the debugger) that the "rowToUpdate" is correct and holds the correct data.

    Why is the update not working? Is it not convinced that the row has changed (yes - the fields have been changed)?

    Or am I missing a database commit? or what? 

    Thanks

    Trefor



    • Edited by TreforE Tuesday, February 16, 2016 4:50 PM
    Tuesday, February 16, 2016 4:49 PM

Answers

  • Ahhh

    Found a fix.

    I've added a call to AcceptChanges and SetModified and it works.

    DataRow rowToUpdate = (manufacturerMapDataGridView.CurrentRow.DataBoundItem as DataRowView).Row; rowToUpdate.AcceptChanges();

    if(rowToUpdate.RowState == DataRowState.Unchanged)

                        {

                             rowToUpdate.SetModified();

                         }

    this.manufacturerMapTableAdapter.Update(rowToUpdate);


    Trefor


    • Marked as answer by TreforE Wednesday, February 17, 2016 9:10 AM
    • Edited by TreforE Wednesday, February 17, 2016 9:30 AM
    Wednesday, February 17, 2016 9:10 AM

All replies

  • Hi TreforE,

    As the code you provide, it seems that the method “manufacturerMapTableAdapter.Update” is a custom method which has a DataSet parameter, does it? But this line (as below), you put a DataRow in that method. If not so, could you please provide a bit more information about the custom method and detail error message.

    this.manufacturerMapTableAdapter.Update(rowToUpdate);

    Best regards,

    Cole Wu

    Wednesday, February 17, 2016 2:59 AM
    Moderator
  • Cole

    But I have manufacturerMapTableAdapter.Update with a DataRow parameter is standard.

    Trefor

    Wednesday, February 17, 2016 9:09 AM
  • Ahhh

    Found a fix.

    I've added a call to AcceptChanges and SetModified and it works.

    DataRow rowToUpdate = (manufacturerMapDataGridView.CurrentRow.DataBoundItem as DataRowView).Row; rowToUpdate.AcceptChanges();

    if(rowToUpdate.RowState == DataRowState.Unchanged)

                        {

                             rowToUpdate.SetModified();

                         }

    this.manufacturerMapTableAdapter.Update(rowToUpdate);


    Trefor


    • Marked as answer by TreforE Wednesday, February 17, 2016 9:10 AM
    • Edited by TreforE Wednesday, February 17, 2016 9:30 AM
    Wednesday, February 17, 2016 9:10 AM
  • TreForE, what should also work (and would be better if it does work ... I haven't tested it, but it should work), is to simply do an .EndEdit, rather than AcceptChanges and SetModified:

    DataRow rowToUpdate = (manufacturerMapDataGridView.CurrentRow.DataBoundItem as DataRowView).Row;
    rowToUpdate.EndEdit();                     
    this.manufacturerMapTableAdapter.Update(rowToUpdate);

    Give that a try ...


    ~~Bonnie DeWitt [C# MVP]

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

    Sunday, February 28, 2016 6:05 PM