EndEdit on BindingSource updates DataTable, but rowstate still unchanged

19,543

Solution 1

UPDATE:

Don't know why, but it seems that calling the specific row's EndEdit does the trick. The row's parent is a datatable and the table's dataset is the datasource of the bsSending Datasource. Calling bsSending.EndEdit() only updates the values but doesn't update the rowstate.

I have surfed the .net for similiar problems and they indicate that when calling AcceptChanges() on the dataset BEFORE binding data, then you may get this error (values updated to dataset but rowstate remains unchanged). I haven't seen any solutions to the problem, though, so I keep my workaround solution

Solution 2

I know it's old post. It can be solved by calling DataRowView.EndEdit directly, but in my case I found the exact reason:

I accidentally bound two properties of one control to different columns. (In my case I simultaneously bound Devexpress' TextEdit control - EditValue and Text properties to different columns of the underlaying table).

Maybe it will help someone even in 2016+, because it was nasty bug to catch.

Solution 3

I have run into the similar situation in one form and I realized that problem was in binding made to labels ToolTip property. First I realized, that when I changed value for null from null to some string, the problem disappears, but after some other changes made to the project, the problem appeared again. When I deleted binding to the ToolTip property, problem disappeared again. Still I do not know if for ever.

Solution 4

I have a similar issue. I have a grid on the first tab page and textboxes on the second tab page, all binded to the same binding source. I change the row content, the content get changed in the grid but the row start is Unchanged.

My code was:

DataRow dataRow =  ((DataRowView)bindingSource1.Current).Row;
if(dataRow.RowState != DataRowState.Modified)

I expected to have a Modified rowState.

The missing code was:

bindingSource1.EndEdit();

Entire solution here:

private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
{
    if (tabControl1.SelectedIndex == 0)
    {
        bindingSource1.EndEdit();
        DataRow dataRow =  ((DataRowView)bindingSource1.Current).Row;
        if(dataRow.RowState != DataRowState.Modified)
        {
            return;
        }
        DialogResult userOption = MessageBox.Show("Save?", "Confirm", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
        if (userOption == System.Windows.Forms.DialogResult.Yes)
        {
            Save();
        }
    }
}

My solution:

Solution 5

@Jan Strnad 's answer enlightened me and helped me fix my issue.

Problem: I was using a binding source in win forms. When binding the controls in the form, I miss clicked and bound a field to the form's Text property in (DataBindings). The same bindingsource field was bound (correctly) to a combobox. Because of this double binding on the field, the rowstate never changed from Unmodified.

Fix: when rows don't update and you use a binding source, first check that the binds are correctly put in place.

Share:
19,543
Martin
Author by

Martin

Developer and project manager, currently at Axia AS. Loves football, snooker, tennis, ski, guitar technology and music and like being updated @martinhelgesen

Updated on June 05, 2022

Comments

  • Martin
    Martin almost 2 years

    I have a bindingsource which has a datasource which is a datatable.
    All the winforms controls has added databindings to the bindingsource

    I do a value change in the GUI (writes to the controls' .text property)

    then on Save i do following

      bsSending.EndEdit();      
    

    ((DataRowView)this.bsSending.Current).Row) now contains the new values, but the RowState is still unchanged. How can this be possible? I haven't any calls to AcceptChanges() before I make the value changes in the GUI