Link to home
Start Free TrialLog in
Avatar of Eric Christianson
Eric ChristiansonFlag for United States of America

asked on

WinForms, DataSets, and C# - Passing DataSet bewteen two Forms

MS Visual Studio 2005, SQL 2005, C#, XP Pro, WinForms

I am an ASP.Net/C# programmer with a WinForms Question:  I have two forms, A and B.  The user navs from Form A to Form B.  In Form B, I retrieve a dataset from SQL, and I give it a name (dsPrint).  The user modifies the dataset via a DataGrid in Form B.  I do not save it to SQL.  The user then returns to Form A to View and Print from the DataSet.

Question: Can you give me an example of how I identify the dsPrint dataset in Form A, so I can bind it, scan through it, etc?



Avatar of dungla
dungla
Flag of Viet Nam image

1. In form A declare a DataSet with your name (ex: myDataSet).
2. In form B make DataSet dsPrint become a property with get like this:
private DataSet dsPrint;
public DataSet PrintData
{
get {return dsPrint;}
}
3. Whenever user return to form A, just retrieve the PrintData from form B
FormB formB = new FormB();
formB.ShowDialog();
myDataSet = formB.PrintData
Avatar of Eric Christianson

ASKER

Something is wrong.  Your code compiles - but - Here are my results:

Form A
protected DataSet myDataSet;

Form B
private DataSet dsPrint;
..
public DataSet PrintData
{
       get {return dsPrint;}
}

Form A (Print Button method)
formB FormB = new formB();
// FormB.ShowDialog();  (Commented out - no effect on results)
myDataSet = FormB.PrintData;

Problem:  During the Initialization of Form B, I retrieve a dataset that has 3 tables, the first one is empty, the others fill comboboxes.  The user selects several filters and I again retrieve the dataset, this time the first table is full.  The user selects what they want to print via a datagrid with imbedded checkboxes.  I delete the unchecked rows from the dataset, and return to Form A with the modified dataset.  All is good.  When the user presses the Print button on Form A, I run your (two) lines above.  For whatever reason, the data retrieval method on FORM B gets run again, empties out the dataset's first table and returns - overwriting the content modified by the user with an empty table.
I've tried adding code to bypass the data retrieval method in Form B, but this causes additional problems.
What am I missing here?
ASKER CERTIFIED SOLUTION
Avatar of dungla
dungla
Flag of Viet Nam 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
Beauty - Thank you sir.