0

是否可以检索已经填充的数据Dataset?示例我Customer从 a 中填充了数据集“” form1,然后我想再次检索数据集“ Customerform2而不执行 SQL 查询。

4

3 回答 3

0

Create a DataSet property on the second form and pass the values from form1's dataset to this property.

public class Form1
{
    public DataSet ds;

    // You have already filled your DataSet so I'll leave that code out
    public void ShowForm2()
    {
        Form2 frm = new Form2();
        frm.MyDataSet = ds;
        frm.Show();
    }
}

public class Form2
{
    public DataSet MyDataSet { get; set; }
}
于 2013-01-14T08:58:45.720 回答
0
public class Form1
{
    private DataSet _myDataSet;
    // do things
    private void fillMyDataSet()
    {
        //fill your dataset
    }
    public dataSet GetMyDataSet()
    {
        if(_myDataSet != null)
            return _myDataSet;
        else
        {
            return null;
        }
    }
}

然后在您的表格 2 中,您所要做的就是:

DataSet myOtherDataSet = Form1.GetMyDataSet();
于 2013-01-14T09:01:14.857 回答
0

您可以将其存储在的属性中Form1并将其实例传递Form1Form2

public class Form1:Form 
{
    public DataSet Data { get; set; }

    public void ShowForm2()
    {
        Form2 child = new Form2(this);
    }
}

public class Form2 : Form
{
    public Form2(Form1 parent) { Parent = parent; }
    public Form1 Parent { get; set; }

    public void SomeMethod()
    {
        // now you can use the DataSet of Form1 via Parent proprty:
        DataSet data = this.Parent.Data;
    }
}
于 2013-01-14T09:05:51.493 回答