Application level Dataset and the form designer  
Author Message
_Roland





PostPosted: .NET Framework Data Access and Storage, Application level Dataset and the form designer Top

What is the best way to do an application level dataset that all forms can share and at and not break the form designer

< xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /> 

Using the DataSource to drag controls on really works nice but the dataset is local to the form. However if you point the dataset of the form to point to a Global/Application dataset the form designer does not work.

 

I have used conditional compilation which seems like work around but you have to add and remove the conditional flag, kind of a pain.

 

private void InitializeComponent()

{

...

#if Designer           

            this.dataSet = new DataSetTest.DataSetTestDataSet();

#else

            this.dataSet = Program.ProgramDataSet;

#endif

 ...

 

}

Or is there a better way

 

Thanks



.NET Development24  
 
 
Sohail Iqbal





PostPosted: .NET Framework Data Access and Storage, Application level Dataset and the form designer Top

Declare dataset with internal modifier in MDI form pass this argument in the contructor of child forms. For this you can create user defined contructor in other forms to accept dataset as parameter. Assign this dataset to the form level variable in the form so that its visible in that form.

public partial class MDIForm : Form

{

internal DataSet MainDataSet;

..................................

other methods

................................

private void btnShowChild_Click(object sender, EventArgs e)

{

ChildForm child = new ChildForm(MainDataSet);

child .MdiParent = this;

child .Show();

}

}

public partial class ChildForm : Form

{

private DataSet refMainDataSet;

public ChildForm(DataSet dataSet)

{

InitializeComponent();

this.refMainDataSet = dataSet;

//Now u can use refMainDataSet in child form and same way in other forms as well and it'll be global for other forms as it is declared in MDI Form.

}

}

 

Cheers.



 
 
BonnieB





PostPosted: .NET Framework Data Access and Storage, Application level Dataset and the form designer Top

I'd just take the dataset off of the form design surface and just add it myself. All that this changes is that the DataSet won't be initialized in InitializeComponent() and you do it yourself in the constructor like this:

public class MyForm : System.Windows.Forms.Form

{

   private DataSet dataSet;

   public MyForm

   {

      this.dataSet = Program.ProgramDataSet;

      InitializeComponent();

   }

}



 
 
_Roland





PostPosted: .NET Framework Data Access and Storage, Application level Dataset and the form designer Top

Thanks,< xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

The suggestion is a good one and correct except the form designer does not like it.

As near as I can tell the form designer needs to create its own instances of the variable.

I tried another approach

 

public TestControl()

        {

           

            InitializeComponent();            

        }

        public void Init()

        {

            // code repated from InitializeComponent();

this.dataSetTestDataSet = Program.ProgramDataSet;

            this.unitsTableAdapter.Fill(this.dataSetTestDataSet.Units);

            this.unitFamilyBindingSource.DataSource = this.dataSetTestDataSet;

        }

 

 

CallingForm

      TestControl t = new TestControl();

      t.Init();

This does work but it but it is repeating code.

 

I am still open to suggestions.


 
 
Sohail Iqbal





PostPosted: .NET Framework Data Access and Storage, Application level Dataset and the form designer Top

form designer does not like it.

I didn't get this. I tested the code it was working fine.

This does work but it but it is repeating code.

If you mean that since you dragged and dropped control on the form and they are initialized twice once by form and second by you then there is better way. Don't drag n drop, create variables from classes and initialize in your code.

If this is not what you have asked for then please clarify.



 
 
BonnieB





PostPosted: .NET Framework Data Access and Storage, Application level Dataset and the form designer Top

Roland,

>As near as I can tell the form designer needs to create its own instances of the variable.<

I doubt that, even though I've not played around enough with VS2005 yet to know for a fact. I think you simply need to move that code that you're duplicating to the Form's Load eventhandler instead of having it in the Constructor.

public class TestControl

{

DataSet this.dataSetTestDataSet;

public TestControl()

{

InitializeComponent();

}

public void TestControl_Load(object sender, System.EventArgs e)

{

this.dataSetTestDataSet = Program.ProgramDataSet;

this.unitsTableAdapter.Fill(this.dataSetTestDataSet.Units);

this.unitFamilyBindingSource.DataSource = this.dataSetTestDataSet;

}

}



 
 
LeoFromCanada





PostPosted: .NET Framework Data Access and Storage, Application level Dataset and the form designer Top

I'd just take the dataset off of the form design surface and just add it myself. All that this changes is that the DataSet won't be initialized in InitializeComponent() and you do it yourself in the constructor like this:

I actually run into the same problem. I can create _global_ dataset and use it across all the other forms, but in that case all the nice Designer's features are gone.

Let say I have grid which I can customize in the designer only if I have dataset object in the designer view. If I remove it from designer view, I should do all binding, customization etc manually, which is a pain.

So the question is: Is there any way to have global dataset while still using Designer

Thanks,

Leo