2012-06-13 119 views
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= 

     } 




    } 
} 

回答

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]; 
+0

好的...我在表单加载方法中移动了我的代码。但仍然没有在表格中显示。我将网格的数据源指定为数据集对象,但仍然没有结果。还有什么我失踪?感谢你的帮助。 –

+0

再次感谢...现在问题已解决。告诉我一两件事,是有可能与内存中的数据的基础工作,而从一个文本框来填充数据表获取用户输入的?我试过了,没有显示错误,但也没有显示该列中的数据? –

+0

现在一切都似乎是工作,感谢一切。 –

0

您的数据集

Application.Run(new Form1()); 

移动这条线下创建数据集的前下方创建窗体。

0

像普里特僧伽说,你创建和运行数据集之前的形式。

此外,该数据集是你的主要方法内部的局部变量,以便窗体类具有毫不知情。尝试在表单中创建数据集或将其作为参数传递给构造函数:

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


... 

Application.Run(new Form1(ds));