Link to home
Start Free TrialLog in
Avatar of Camillia
CamilliaFlag for United States of America

asked on

Datatable - only keep the rows that have been changed

This is Windows Forms, C#.

I have a Dataset with 3 datatables.  For datatable ST_MER_POS_Product_Online_List_AA, I want to only keep the rows that have been changed.

1. This is is how I check if the datatable has rows changed:
                  var ttest = IsDataSetChanged(Onlineds); //**** check to see if row is changed
                //var p = Onlineds.ST_MER_POS_Product_Online_List;

               ws.UpdateOnlineProducts_AA(Authorize.MAC_IDs, Onlineds); //call to update 
                this.Onlineds.AcceptChanges();

Open in new window


2. Check to see if row has changed or not
 public bool IsDataSetChanged(DataSet m_DSet)
        {
            if (m_DSet == null)
                return (false);

            foreach (DataTable dt in m_DSet.Tables)
            {
                foreach (DataRow dr in dt.Rows)
                {
                    if (dr.RowState != DataRowState.Unchanged)
                        return (true);
                    
                }
            }
            return (false);
        }

Open in new window


3. I  tried dr.Delete(); but I think it deleted all the rows

 public DataSet IsDataSetChangedOnlineProduct(DataSet m_DSet)
        {
            if (m_DSet != null)
            {
                foreach (DataTable dt in m_DSet.Tables)
                {
                    //var p = dt.Rows.Count;

                  //  var p = dt.GetChanges();

                    foreach (DataRow dr in dt.Rows)
                    {
                       if (dr.RowState == DataRowState.Unchanged) 
                       {
                          dr.Delete();

                       }



                    }
                }

                m_DSet.AcceptChanges();

                return m_DSet;
            }


            return m_DSet;
        }

Open in new window


How can I do this?
ASKER CERTIFIED SOLUTION
Avatar of Chinmay Patel
Chinmay Patel
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Camillia

ASKER

ah, let me see. Thanks
"delete" removes the row from the datasource ( database table) but "remove" only removes it from the datatable collection, correct?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Now I get it. Thanks for the explanation :)
How do I handle this? I'm getting this in the loop. Should I be doing "AccpetChanges()" in the loop? (I'll try it after this post)

I only want to keep the rows that are changed.

  public DataSet IsDataSetChangedOnlineProduct(DataSet m_DSet)
        {
            if (m_DSet != null)
            {
                foreach (DataTable dt in m_DSet.Tables)
                {
                
                    foreach (DataRow dr in dt.Rows)
                    {
                        if (dr.RowState != DataRowState.Unchanged)
                        {
                           dt.Rows.Remove(dr);

                           //var p = dr;

                        }

                      
                    }
                }

              //  m_DSet.AcceptChanges();

                return m_DSet;
            }
            
            return m_DSet;
        }

Open in new window


User generated image
I would move AcceptChanges after I have processed everything.
in the loop?
I might have to create another collection. Let me try that.
Thanks, Chinmay. This answers my question but I have another issue that doesn't make sense. I'll open a new question.