none
What Event should I use to update a row? RRS feed

  • Question

  • I have forms over data application in a typical WinFormControl->BindingSource->DataSet.DataTable configuration. I have it broken out into layers for the UI, DAL, and BL.  Each of the main SQL tables has a "CreatedDate", "ModifiedDate" and "ModifiedBy" column along with the data fields.  The CreatedDate, ModifiedDate, and ModifiedBy fields for the various tables are not displayed or part of the UI.  I would like to update those fields with calculated values when the user modifies a row for any of the tables. 

    My question is when and where should I update fields that are not databound on the UI?  I have been using the underlying bindingsource to add/delete rows and the TableAdapters to update the backend. I thought perhaps to grab the GetChanges copy of the DataSet and loop through the tables updateding the fields prior to saving.  However, I see from a posts here that it might cause problems updating the copy that is returned with a GetChanges.  The bindingsource has a few events that might work but none of them looked like the right way to go.  I'm not sure how to wire up the row changed event using the business layer as I have not added any business logic yet. 

    DeBug  

    Monday, April 26, 2010 8:04 PM

Answers

  • Hi,

    We could use DataTable's RowChanged event to modify the other columns not showed in the UI. Please refer to the following sample code:

    public partial class Form1 : Form
    {
      public Form1()
      {
        InitializeComponent();
      }
    
      private void modifiedDateTestBindingNavigatorSaveItem_Click(object sender, EventArgs e)
      {
        this.Validate();
        this.modifiedDateTestBindingSource.EndEdit();
        this.modifyDate1BindingSource.EndEdit();
    
        foreach (DataTable dt in this.northwindDataSet.Tables)
        {
          dt.RowChanged -= dt_RowChanged; //remove row changed event handler for each table in dataset to void row changed again        
        }
    
        this.modifiedDateTestTableAdapter.Update(this.northwindDataSet.ModifiedDateTest);      
        this.modifyDate1TableAdapter.Update(this.northwindDataSet.ModifyDate1);
    
        foreach (DataTable dt in this.northwindDataSet.Tables)
        {
          dt.RowChanged += dt_RowChanged; //Add row changed event handler for each table in dataset again        
    
        }
      }
    
      private void Form1_Load(object sender, EventArgs e)
      {
        // TODO: This line of code loads data into the 'northwindDataSet.ModifyDate1' table. You can move, or remove it, as needed.
        this.modifyDate1TableAdapter.Fill(this.northwindDataSet.ModifyDate1);
        // TODO: This line of code loads data into the 'northwindDataSet.ModifiedDateTest' table. You can move, or remove it, as needed.
        this.modifiedDateTestTableAdapter.Fill(this.northwindDataSet.ModifiedDateTest);   
    
        foreach (DataTable dt in this.northwindDataSet.Tables)
        {
          dt.RowChanged += new DataRowChangeEventHandler(dt_RowChanged); //Add row changed event handler for each table in dataset        
        }
      }
    
      void dt_RowChanged(object sender, DataRowChangeEventArgs e)
      {
        DataTable dt = (DataTable)sender;
        dt.RowChanged -= dt_RowChanged; // we must remove the event handler when we modify the "ModifiedDate" column, otherwise, infinite loop will occur
        e.Row["ModifiedDate"] = DateTime.Now;
        dt.RowChanged += dt_RowChanged; // add the event handler again
      }
    }
    

    Best regards,
    Alex Liang


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Thursday, April 29, 2010 9:35 AM
    Moderator

All replies

  • Hi,

    We could use DataTable's RowChanged event to modify the other columns not showed in the UI. Please refer to the following sample code:

    public partial class Form1 : Form
    {
      public Form1()
      {
        InitializeComponent();
      }
    
      private void modifiedDateTestBindingNavigatorSaveItem_Click(object sender, EventArgs e)
      {
        this.Validate();
        this.modifiedDateTestBindingSource.EndEdit();
        this.modifyDate1BindingSource.EndEdit();
    
        foreach (DataTable dt in this.northwindDataSet.Tables)
        {
          dt.RowChanged -= dt_RowChanged; //remove row changed event handler for each table in dataset to void row changed again        
        }
    
        this.modifiedDateTestTableAdapter.Update(this.northwindDataSet.ModifiedDateTest);      
        this.modifyDate1TableAdapter.Update(this.northwindDataSet.ModifyDate1);
    
        foreach (DataTable dt in this.northwindDataSet.Tables)
        {
          dt.RowChanged += dt_RowChanged; //Add row changed event handler for each table in dataset again        
    
        }
      }
    
      private void Form1_Load(object sender, EventArgs e)
      {
        // TODO: This line of code loads data into the 'northwindDataSet.ModifyDate1' table. You can move, or remove it, as needed.
        this.modifyDate1TableAdapter.Fill(this.northwindDataSet.ModifyDate1);
        // TODO: This line of code loads data into the 'northwindDataSet.ModifiedDateTest' table. You can move, or remove it, as needed.
        this.modifiedDateTestTableAdapter.Fill(this.northwindDataSet.ModifiedDateTest);   
    
        foreach (DataTable dt in this.northwindDataSet.Tables)
        {
          dt.RowChanged += new DataRowChangeEventHandler(dt_RowChanged); //Add row changed event handler for each table in dataset        
        }
      }
    
      void dt_RowChanged(object sender, DataRowChangeEventArgs e)
      {
        DataTable dt = (DataTable)sender;
        dt.RowChanged -= dt_RowChanged; // we must remove the event handler when we modify the "ModifiedDate" column, otherwise, infinite loop will occur
        e.Row["ModifiedDate"] = DateTime.Now;
        dt.RowChanged += dt_RowChanged; // add the event handler again
      }
    }
    

    Best regards,
    Alex Liang


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Thursday, April 29, 2010 9:35 AM
    Moderator
  • Hi Alex,

    Thank you for your reply.  I'm still looking at why you need to enable and disable the events but will give this a try.

     

    DeBug

    Monday, May 3, 2010 6:41 PM