none
DatarowState not updating when value being updated via binded textbox.

    Question

  • Hi,
    I have an application in which I am binding a DataTable's column value with textbox's text property as follows:
    textBox1.DataBindings.Add("Text", dtSource, "Field1", true, DataSourceUpdateMode.OnPropertyChanged);
    textBox2.DataBindings.Add(
    "Text", dtSource, "Field2", true, DataSourceUpdateMode.OnPropertyChanged);

    When I make any changes to textbox's text property, its automaticaly updating Datatable's values.
    But still I am getting RowState as UnChanged after update as follows:
    MessageBox.Show("RowState: " + Convert.ToString(dtSource.Rows[0].RowState.ToString()));

    Please help me providing a way, so that I get exact RowState as Modified.

    Regards,
    Karan
    Thursday, August 13, 2009 7:49 PM

Answers

  • Karan,

    Data in a DataRow has several different versions. First, there's the original version. Then, when it's being edited, it has a Proposed version and once it's done being edited, that becomes the Current version. Sometimes when editing, the row is left in the Proposed state and the Edit needs to be ended programmatically.

    Here's a method I *always* call before I attempt to check for .HasChanges() before saving data:

    protected virtual void CommitProposedChanges(DataSet ds)
    {
    	if (ds == null)
    		return;
    
    	for (int nTable = 0; nTable < ds.Tables.Count; nTable++)
    	{
    		for (int nRow = 0; nRow < ds.Tables[nTable].Rows.Count; nRow++)
    		{
    			if (ds.Tables[nTable].Rows[nRow].HasVersion(DataRowVersion.Proposed))
    			{
    				ds.Tables[nTable].Rows[nRow].EndEdit();
    			}
    		}
    	}
    }
    

    ~~Bonnie Berent [C# MVP]
    • Marked as answer by Karan Singh Monday, August 17, 2009 10:17 AM
    Sunday, August 16, 2009 2:10 PM

All replies

  • In which event are you calling your messagebox?


    www.insteptech.com ; msmvps.com/blogs/deborahk
    We are volunteers and ask only that if we are able to help you, that you mark our reply as your answer. THANKS!
    Thursday, August 13, 2009 10:48 PM
  • Thanks for the prompt reply. Actually I am not gonna use messagebox, its just for test. There is button control on click of which I need to save changes in the datasource, if rowstate is Modified. Since rowstate is unchanged, its not saving anything. Regards, Karan
    Friday, August 14, 2009 4:38 AM
  • Karan,

    Data in a DataRow has several different versions. First, there's the original version. Then, when it's being edited, it has a Proposed version and once it's done being edited, that becomes the Current version. Sometimes when editing, the row is left in the Proposed state and the Edit needs to be ended programmatically.

    Here's a method I *always* call before I attempt to check for .HasChanges() before saving data:

    protected virtual void CommitProposedChanges(DataSet ds)
    {
    	if (ds == null)
    		return;
    
    	for (int nTable = 0; nTable < ds.Tables.Count; nTable++)
    	{
    		for (int nRow = 0; nRow < ds.Tables[nTable].Rows.Count; nRow++)
    		{
    			if (ds.Tables[nTable].Rows[nRow].HasVersion(DataRowVersion.Proposed))
    			{
    				ds.Tables[nTable].Rows[nRow].EndEdit();
    			}
    		}
    	}
    }
    

    ~~Bonnie Berent [C# MVP]
    • Marked as answer by Karan Singh Monday, August 17, 2009 10:17 AM
    Sunday, August 16, 2009 2:10 PM
  • Hi Bonnie,
    It worked fine.
    Thanks a lot for extra information.

    Regards,
    Karan
    Monday, August 17, 2009 10:17 AM
  • You're welcome, Karan! Glad to help. =0)
    ~~Bonnie Berent [C# MVP]
    Monday, August 17, 2009 10:19 PM