0

我已经使用断开连接的类创建了一个数据表,还创建了数据集、xml 文件,现在我想将该表加载到网格中。我在program.cs下编写了所有代码,但是当我尝试从表单加载方法访问数据集对象时,无法识别数据集对象。代码在这里:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.Data;
using System.IO;

namespace DisconnectedClassDemo
{
    public class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]


        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());



            DataTable studentInfo = new DataTable("studentinfo");

            DataColumn StudentID = new DataColumn("studentID");
            StudentID.DataType = typeof(int);
            StudentID.Caption="StudentID";
            StudentID.AutoIncrement = true;
            StudentID.AutoIncrementSeed = 1;
            StudentID.AutoIncrementStep = 1;


            DataColumn Name = new DataColumn("StudentName", typeof(string));
            Name.MaxLength = 50;
            Name.AllowDBNull = false;
            Name.Caption = "StudentName";

            DataColumn Roll = new DataColumn("StudentRoll", typeof(int));
            Roll.Caption = "StudnetRoll";

            studentInfo.Columns.Add(StudentID);
            studentInfo.Columns.Add(Name);
            studentInfo.Columns.Add(Roll);

            studentInfo.PrimaryKey = new DataColumn[] { StudentID };

            //DataRow rowobj = studentInfo.NewRow();
            //rowobj["studentName"] = "Badhon";
            //rowobj["studentRoll"] = "004";

            //studentInfo.Rows.Add(rowobj);


            DataSet ds = new DataSet("dataset");
            ds.Tables.Add(studentInfo);

            ds.WriteXmlSchema("D:\\Student.xsd");
            ds.WriteXml("D:\\student.xml");



            ds.ReadXmlSchema("d:\\student.xsd");
            ds.ReadXml("D:\\student.xml");







        }
    }



}


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace DisconnectedClassDemo
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //dataGridView1.DataSource= 
            //dataGridView1.DataMember=

        }




    }
}
4

3 回答 3

0

为什么要将数据集写入文件系统?您不是在尝试创建内存数据集并访问该数据集吗?尝试在表单本身上声明数据集。

试试这个:

            DataTable studentInfo = new DataTable("studentinfo");

        DataColumn StudentID = new DataColumn("studentID");
        StudentID.DataType = typeof(int);
        StudentID.Caption = "StudentID";
        StudentID.AutoIncrement = true;
        StudentID.AutoIncrementSeed = 1;
        StudentID.AutoIncrementStep = 1;


        DataColumn Name = new DataColumn("StudentName", typeof(string));
        Name.MaxLength = 50;
        Name.AllowDBNull = false;
        Name.Caption = "StudentName";

        DataColumn Roll = new DataColumn("StudentRoll", typeof(int));
        Roll.Caption = "StudnetRoll";

        studentInfo.Columns.Add(StudentID);
        studentInfo.Columns.Add(Name);
        studentInfo.Columns.Add(Roll);

        studentInfo.PrimaryKey = new DataColumn[] { StudentID };

        DataSet ds = new DataSet("dataset");
        ds.Tables.Add(studentInfo);

        var rw = ds.Tables[0].NewRow();

        rw["StudentName"] = "Badhon";
        rw["StudentRoll"] = 004;

        ds.Tables[0].Rows.Add(rw);

        MessageBox.Show(ds.Tables[0].Rows.Count.ToString());

        dataGridView1.DataSource = ds.Tables[0];
于 2012-06-13T03:04:10.243 回答
0

您在数据集之前创建表单

Application.Run(new Form1()); 

将此行向下移动到数据集的创建下方。

于 2012-06-13T03:04:37.810 回答
0

就像 Preet Sangha 所说,您在数据集之前创建和运行表单。

此外,数据集是 Main 方法中的局部变量,因此表单类不知道它。尝试在表单内创建数据集或将其作为参数传递给构造函数:

public class Form1(Dataset ds) : Form 
...


...

Application.Run(new Form1(ds));
于 2012-06-13T03:19:04.110 回答