开发者

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.

  1. Use the properties in the target form and assign it while instantiating its class
  2. Pass the datasource in target form constructor.
  3. You are presently in Form2 and here is another way, Form1.ListBox.YourpropertyName;
  4. Delegates can be used, but it cost memory a lot also should be disposed after use.
  5. 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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜