Access data controls between different forms in C#
XmlDataDocument xmlDatadoc =开发者_高级运维 new XmlDataDocument();
xmlDatadoc.DataSet.ReadXml(dir + listBox1.SelectedItem);
DataSet ds = new DataSet("Customer info");
ds = xmlDatadoc.DataSet;
dataGridView1.DataSource = ds.DefaultViewManager;
dataGridView1.DataMember = "Customer";
Now If the ListBox control "listBox1" is located in a different form say "form1" of the application, how can I get the data & use it in a datagrid in "Form2"?
According to my knowledge , following should be the ways.
- Use the properties in the target form and assign it while instantiating its class
- Pass the datasource in target form constructor.
- You are presently in Form2 and here is another way, Form1.ListBox.YourpropertyName;
- Delegates can be used, but it cost memory a lot also should be disposed after use.
- Create a class with static member and initialize this before going to target form and access this value in the target form
Moreover, The Access modifier of the control should be appropriate to access in other forms
Please note that the Point 3 is valid in case your previous form is in open state. Otherwise it will show you null data in the Listbox and In point 5, static variable memory should be set to null once used.
精彩评论