• 我学datatable


    DataSet    http://msdn.microsoft.com/zh-cn/library/bwy42y0e(v=VS.80).aspx

    DataTtable   http://msdn.microsoft.com/zh-cn/library/system.data.datatable(VS.80).aspx

      DataColumn  行  http://msdn.microsoft.com/zh-cn/library/8a7cyy8d(v=VS.80).aspx   

        DataType   数据类型

        ColumnsName   列名

        ReadoyOnly     获取或设置一个值,该值指示一旦向表中添加了行,列是否还允许更改

        Unique           获取或设置一个值,该值指示列的每一行中的值是否必须是唯一的。

        AutoIncrement  获取或设置一个值,该值指示对于添加到该表中的新行,列是否将列的值自动递增

        Caption            获取或设置列的标题

         DataRow  列   http://msdn.microsoft.com/zh-cn/library/system.data.datarow(VS.80).aspx

    // Put the next line into the Declarations section.
    private System.Data.DataSet dataSet;
     
    private void MakeDataTables()
    {
        // Run all of the functions. 
        MakeParentTable();
        MakeChildTable();
        MakeDataRelation();
        BindToDataGrid();
    }
     
    private void MakeParentTable()
    {
        // Create a new DataTable.
        System.Data.DataTable table = new DataTable("ParentTable");
        // Declare variables for DataColumn and DataRow objects.
        DataColumn column;
        DataRow row;
     
        // Create new DataColumn, set DataType, 
        // ColumnName and add to DataTable.    
        column = new DataColumn();
        column.DataType = System.Type.GetType("System.Int32");
        column.ColumnName = "id";
        column.ReadOnly = true;
        column.Unique = true;
        // Add the Column to the DataColumnCollection.
        table.Columns.Add(column);
     
        // Create second 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;
        // Add the column to the table.
        table.Columns.Add(column);
     
        // Make the ID column the primary key column.
        DataColumn[] PrimaryKeyColumns = new DataColumn[1];
        PrimaryKeyColumns[0] = table.Columns["id"];
        table.PrimaryKey = PrimaryKeyColumns;
     
        // Instantiate the DataSet variable.
        dataSet = new DataSet();
        // Add the new DataTable to the DataSet.
        dataSet.Tables.Add(table);
     
        // Create three new DataRow objects and add 
        // them to the DataTable
        for (int i = 0; i<= 2; i++)
        {
            row = table.NewRow();
            row["id"] = i;
            row["ParentItem"] = "ParentItem " + i;
            table.Rows.Add(row);
        }
    }
     
    private void MakeChildTable()
    {
        // Create a new DataTable.
        DataTable table = new DataTable("childTable");
        DataColumn column;
        DataRow row;
     
        // Create first column and add to the DataTable.
        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;
    
        // Add the column to the DataColumnCollection.
        table.Columns.Add(column);
     
        // Create second 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);
     
        // Create third 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);
    
        // Create three sets of DataRow objects, 
        // five rows each, and add to DataTable.
        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()
    {
        // DataRelation requires two DataColumn 
        // (parent and child) and a name.
        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 BindToDataGrid()
    {
        // Instruct the DataGrid to bind to the DataSet, with the 
        // ParentTable as the topmost DataTable.
    //1、winform绑定
    datagrid1.SetDataBinding(dataset,"table"); 
    //2、web绑定
    datagrid1.DataSource = dataSet; 
    datagrid1.DataBind();
        }
  • 相关阅读:
    jquery easyui 时间控件的使用
    3101 php 学习推荐
    报到
    《C++语言的设计和演化》摘录
    怀念下以前听摇滚乐的日子
    Maven無法下載依賴時的解決方案
    RDF和Jena RDF API入门(2)
    WEB数据挖掘(五)——Aperture数据抽取(1)
    RDF和Jena RDF API入门(1)
    Ubuntu SVN安装配置十分简单
  • 原文地址:https://www.cnblogs.com/0banana0/p/2056069.html
Copyright © 2020-2023  润新知