How to tell if user has modified data using bindingsource?

后端 未结 12 2187
南方客
南方客 2021-01-05 06:05

I have a DataGridView bound to a bindingsource which is bound to a List. The user clicks a row that goes to a form with textboxes, etc. The textboxes a

12条回答
  •  忘掉有多难
    2021-01-05 06:23

    first make Sure you set DataSourceUpdateMode.OnPropertyChanged

    txrFirstName.DataBindings.Add("Text", bindingSource1, "FirstName", false,DataSourceUpdateMode.OnPropertyChanged);
    

    then add add this code to your movenext click event

     if (((DataRowView)bindingSource1.Current).IsNew)
                    {
                    MessageBox.Show("Current Row IsNew");
                    }
                if (((DataRowView)bindingSource1.CurrencyManager.Current).Row.HasVersion(DataRowVersion.Proposed))
                    {
                    MessageBox.Show("Current Row Modified");
                    DialogResult dialogResult = MessageBox.Show("Current Row Modified", "Some Title", MessageBoxButtons.YesNo);
                    if (dialogResult == DialogResult.Yes)
                        {
                        //do something
                        ((DataRowView)bindingSource1.CurrencyManager.Current).Row.AcceptChanges();
                        }
                    else if (dialogResult == DialogResult.No)
                        {
                        //do something else
                        ((DataRowView)bindingSource1.CurrencyManager.Current).Row.RejectChanges();
                        }
    
    
                    }
                else { 
                    bindingSource1.MoveNext();
                    }
    

提交回复
热议问题