DataSet vs. DataView - How to update?

D

deko

I have a WinForms app that builds a DataSet from an XML file. The DataSet
is wrapped in a DataView and bound to various controls through which users
will make changes to the data, and when the "Save and Close" button on my
form is clicked, the changes persist back to XML using XmlDataDocument.

My question is this: Should I update the DataView or the DataSet?

An example of updating the DataSet might look like this:

public static void newLst(DataSet xdd)
{
DataView dv = null;
dv = new DataView (
xdd.Tables["Configuration"],
"ProjectName = 'UnassignedConfiguration'",
"ConfigurationName",
DataViewRowState.CurrentRows
);
//dv.AllowEdit = true;
//dv.AllowNew = true;
//dv.AllowDelete = true;
//why would I want to use the above methods rather than updating the DataSet
directly?
}

An example of updating the DataSet might look like this:

public static void chgNam
(
DataSet xdd,
string oldNam,
string newNam,
string tblNam
)
{
DataTable dt = xdd.Tables[tblNam];
DataRow dr = dt.Rows.Find(oldNam);
dr[tblNam + "Name"] = newNam;
}

Suggestions or comments welcome!

Thanks in advance.
 
W

W.G. Ryan eMVP

You'd want to use those methods becuase anything you do to the DataView will
be done to the underlying table and you will want to bind to a filtered view
in many instances, instead of a dataTable.

Just update the dataTable that the view is based on and you'll be in good
shape (Update as in the DataAdapter) for client side editing, editing the
view will be fine.
 
D

deko

You'd want to use those methods becuase anything you do to the DataView
will
be done to the underlying table and you will want to bind to a filtered view
in many instances, instead of a dataTable.

Just update the dataTable that the view is based on and you'll be in good
shape (Update as in the DataAdapter) for client side editing, editing the
view will be fine.

Hi and thanks for the reply. But I'm a bit confused. In your first
paragraph you seem to advocate updating the DataView with methods such as:

dv.AllowEdit = true;
dv.AllowNew = true;
dv.AllowDelete = true;

But in your second paragraph you suggest that I should "Just update the
dataTable that the view is based on" - with methods such as:

DataTable dt = xdd.Tables[tblNam];
DataRow dr = dt.Rows.Find(oldValue);
dr["RowName"] = newValue;

Sorry if I've missed your meaning... can you please clarify? It would seem
to me that since the DataView is simply a wrapper for the DataTable that
updating the DataTable directly (my second example) would be better. But
then how do I refresh the DataView to which the controls are bound? Would I
use something like this:

public static void newLst(DataSet xdd)
{
DataView dv = null; //dump the old DataView
dv = new DataView //create new DataView
(
xdd.Tables["TableName"],
"RowName" = 'myRow'",
"SortByValue",
DataViewRowState.CurrentRows
);
}

Thanks for your help!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top