zoukankan      html  css  js  c++  java
  • 我学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();
        }
  • 相关阅读:
    http返回码
    JAVA获取当前系统时间System.currentTimeMillis()以及获取运行时间
    【Jsoup】Jsoup解析Html标签(Java后台解析)
    字节、字、位、比特,这四者之间的关系
    Java中字符编码和字符串所占字节数 .
    判断一个字符串是否为全英文的工具
    汉语转拼音(全转与只转首个字母)工具类
    Page的生命周期及相关事件苛
    PHP设计模式——策略模式
    java生产者消费者问题代码分析
  • 原文地址:https://www.cnblogs.com/0banana0/p/2056069.html
Copyright © 2011-2022 走看看