Camillia
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:
2. Check to see if row has changed or not
3. I tried dr.Delete(); but I think it deleted all the rows
How can I do this?
I have a Dataset with 3 datatables. For datatable ST_MER_POS_Product_Online_
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();
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);
}
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;
}
How can I do this?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
"delete" removes the row from the datasource ( database table) but "remove" only removes it from the datatable collection, correct?
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Now I get it. Thanks for the explanation :)
ASKER
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.
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;
}
I would move AcceptChanges after I have processed everything.
ASKER
in the loop?
ASKER
I might have to create another collection. Let me try that.
ASKER
Thanks, Chinmay. This answers my question but I have another issue that doesn't make sense. I'll open a new question.
ASKER