none
WIinForm中怎样将方法中定义的Dataset绑定到DataGridview? RRS feed

  • 问题

  • //下面的示例创建两个 DataTable 对象和一个 DataRelation 对象,并将这些新对象添加到 DataSet 中。这些表随后会显示在 DataGridView 控件中
            public  System.Data.DataSet dataSet;
    
            public void MakeDataTables()
            {
                MakeParentTable();
                MakeChildTable();
                MakeDataRelation();
               
            }
    
            private void MakeParentTable()
            {
                
                System.Data.DataTable table = new DataTable("ParentTable");
                DataColumn column;
                DataRow row;
    
                  
                column = new DataColumn();
                column.DataType = System.Type.GetType("System.Int32");
                column.ColumnName = "id";
                column.ReadOnly = true;
                column.Unique = true;
                table.Columns.Add(column);
    
                column = new DataColumn();
                column.DataType = System.Type.GetType("System.String");
                column.ColumnName = "ParentItem";
                column.AutoIncrement = false;
                column.Caption = "ParentItem";
                column.ReadOnly = false;
                column.Unique = false;
                table.Columns.Add(column);
    
                DataColumn[] PrimaryKeyColumns = new DataColumn[1];
                PrimaryKeyColumns[0] = table.Columns["id"];
                table.PrimaryKey = PrimaryKeyColumns;
    
                dataSet = new DataSet();
                dataSet.Tables.Add(table);
    
                for (int i = 0; i <= 2; i++)
                {
                    row = table.NewRow();
                    row["id"] = i;
                    row["ParentItem"] = "ParentItem " + i;
                    table.Rows.Add(row);
                }
            }
    
            private void MakeChildTable()
            {
                DataTable table = new DataTable("childTable");
                DataColumn column;
                DataRow row
    
                column = new DataColumn();
                column.DataType = System.Type.GetType("System.Int32");
                column.ColumnName = "ChildID";
                column.AutoIncrement = true;
                column.Caption = "ID";
                column.ReadOnly = true;
                column.Unique = true;
                table.Columns.Add(column);
    
                column = new DataColumn();
                column.DataType = System.Type.GetType("System.String");
                column.ColumnName = "ChildItem";
                column.AutoIncrement = false;
                column.Caption = "ChildItem";
                column.ReadOnly = false;
                column.Unique = false;
                table.Columns.Add(column);
    
                column = new DataColumn();
                column.DataType = System.Type.GetType("System.Int32");
                column.ColumnName = "ParentID";
                column.AutoIncrement = false;
                column.Caption = "ParentID";
                column.ReadOnly = false;
                column.Unique = false;
                table.Columns.Add(column);
                dataSet.Tables.Add(table);
    
                for (int i = 0; i <= 4; i++)
                {
                    row = table.NewRow();
                    row["childID"] = i;
                    row["ChildItem"] = "Item " + i;
                    row["ParentID"] = 0;
                    table.Rows.Add(row);
                }
                for (int i = 0; i <= 4; i++)
                {
                    row = table.NewRow();
                    row["childID"] = i + 5;
                    row["ChildItem"] = "Item " + i;
                    row["ParentID"] = 1;
                    table.Rows.Add(row);
                }
                for (int i = 0; i <= 4; i++)
                {
                    row = table.NewRow();
                    row["childID"] = i + 10;
                    row["ChildItem"] = "Item " + i;
                    row["ParentID"] = 2;
                    table.Rows.Add(row);
                }
            }
    
            private void MakeDataRelation()
            {
                DataColumn parentColumn =
                    dataSet.Tables["ParentTable"].Columns["id"];
                DataColumn childColumn =
                    dataSet.Tables["ChildTable"].Columns["ParentID"];
                DataRelation relation = new
                    DataRelation("parent2Child", parentColumn, childColumn);
                dataSet.Tables["ChildTable"].ParentRelations.Add(relation);
            }
           
            private void Form1_Load(object sender, EventArgs e)
            {
                this.dataGrid1.DataSource=dataSet.Tables["ParentTable"];
                this.dataGrid1.DataMember = "ParentTable";
            }
    显示:对象未实例化

    赵轻盈
    2009年12月21日 8:41

答案

全部回复

  • 你好!
         哪一行报错的?哪个对象没有实例化?
    周雪峰
    2009年12月21日 11:43
    版主
  •  private void Form1_Load(object sender, EventArgs e)
            {
    MakeDataTables();
    
                this.dataGrid1.DataSource=dataSet.Tables["ParentTable"];
                this.dataGrid1.DataMember = "ParentTable";
            }
    

    http://feiyun0112.cnblogs.com/
    2009年12月22日 0:48
    版主
  • 你好,赵轻盈

    看了你的代码我发觉上面部分基本没有错误,错误大概是最后两句
    this.dataGrid1.DataSource=dataSet.Tables["ParentTable"];
    this.dataGrid1.DataMember = "ParentTable";

    因为你已经指定了DataSource为DataTable,那DataMember就没必要设置了。或者这样也行
    this.dataGrid1.DataSource=dataSet;
    this.dataGrid1.DataMember = "ParentTable";

    顺便说一下,C#代码最好发到C#论坛。

    Sincerely,
    Kira Qian


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework!
    2009年12月28日 8:31