简体   繁体   English

c#multiforms DataSet

[英]c# multiforms DataSet

On the first form I have: 在我的第一个表格上:

var dsUni = new DataSet();
dsUni.ReadXml(pathDesc);
frm02.dgv02.DataSource = dsUni.Tables[0];
frm02.ShowDialog();
dsUni.WriteXml(pathDesc); //this line is in the frm02.ClosingEvent

So, on the secondForm I need the same DataSet(dsUni) to writeXml from changed dgv02. 因此,在secondForm上,我需要使用相同的DataSet(dsUni)来更改dgv02中的writeXml。
But, i got the errors: 但是,我得到了错误:
The name 'dsUni' does not exist in the current context “dsUni”这个名称在当前上下文中不存在
Please, give me a solution for this case. 请给我一个这个案例的解决方案。

The error is telling you that the second form doesn't have a variable named dsUni defined. 该错误告诉您第二种形式没有名为dsUni的变量。 You need to define it, such as var dsUni = new DataSet(); 您需要定义它,例如var dsUni = new DataSet(); did on the first form. 在第一个表格上做了。 While you have passed the data itself along via the line frm02.dgv02.DataSource = dsUni.Tables[0]; 虽然您已经通过frm02.dgv02.DataSource = dsUni.Tables[0];行传递了数据本身frm02.dgv02.DataSource = dsUni.Tables[0]; , the variable named dsUni is local to form1, so form2 doesn't know about it. ,名为dsUni的变量是form1的本地变量,因此form2不知道它。 If you want to access that dataset under the name dsUni , you must expose it so form2 can directly access it, or create a new local variable by that name on form2 and load it with your desired data (very inefficent, unless you just use a pointer back to form1's dsUni). 如果要以dsUni名称访问该数据集,则必须将其公开,以便form2可以直接访问它,或者在form2上通过该名称创建一个新的局部变量并使用所需数据加载它(非常无效,除非你只使用一个指针返回form1的dsUni)。

If you create a new variable on form2 called dsUni , don't forget to load it with your desired data, as it will start out empty. 如果你在form2上创建一个名为dsUni的新变量,不要忘记用你想要的数据加载它,因为它将从空开始。

Don't do this it's just asking for it. 不要这样做只是要求它。

Huge amount of technical debt justwaiting to happen. 即将发生的大量技术债务。

There's a few ways to do it better, but this one you can build on. 有几种方法可以做得更好,但是你可以建立这个方法。

Create a little class eg 创建一个小班,例如

public class MyData()
{
   public Dataset MyDataset {get; private set;}

   // add constructor and all methods related to the dataset here. 
}

Then just create one in Form1, do what you have to do and pass it to form2, either as a property or in the constructor. 然后只需在Form1中创建一个,执行您必须执行的操作并将其作为属性或构造函数传递给form2。

If it's common and it's not totally trivial, create a common something, put all the methods that relate to it in there and then use it. 如果它是常见的并且它不是完全无关紧要的,那么创建一个常见的东西,将所有与之相关的方法放在那里,然后使用它。 As soon as you start with the form2 depends on form1 manouevre you might as conserve energy and just jam a biro up your left nostril as hard as you can, the headache will be similar. 一旦你开始使用form2取决于form1 manouevre你可能会节省能量,只是尽可能地将你的左鼻孔堵塞,但头痛将是相似的。

An interface will be better, but that's the next lesson. 界面会更好,但这是下一课。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM