Default.aspx

View Code
<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>无标题页</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:GridView ID="GridView1" runat="server">
        </asp:GridView>
    </div>
    </form>
</body>
</html>

Default.aspx.cs

View Code
using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
        MakeMasterTable();  //创建主表
        MakeDetailTable();  //创建子表
        MakeDataRelation(); //建立主子表关联
    }

    private DataSet ds;

    private void MakeMasterTable()
    {
        //创建数据表对象
        DataTable table = new DataTable("MasterTable");
        DataColumn column;
        DataRow row;
        //创建数据列对象
        column = new DataColumn();
        //设置数据列的数据类型
        column.DataType = System.Type.GetType("System.Int32");
        column.ColumnName = "DictTypeID";//指定数据列名
        column.ReadOnly = true;          //设置数据列为只读列
        column.Unique = true;            //设置数据列中的值不允许重复
        table.Columns.Add(column);

        column = new DataColumn();
        column.DataType = System.Type.GetType("System.String");
        column.ColumnName = "DictTypeName";
        column.AutoIncrement = false;    //设置数据列中的值不自动递增
        column.Caption = "DictTypeName"; //设置数据列标题
        column.ReadOnly = false;
        column.Unique = false;
        table.Columns.Add(column);
        //创建列数组
        DataColumn[] PrimaryKeyColumns = new DataColumn[1];
        //将DictTypeID赋给列数组
        PrimaryKeyColumns[0] = table.Columns["DictTypeID"];
        table.PrimaryKey = PrimaryKeyColumns;//设置DictTypeID字段为主键

        ds = new DataSet();//创建数据集对象
        ds.Tables.Add(table);//将上面创建的数据表对象添加表数据集中
        //为数据表添加数据
        for (int i = 0; i< 2; i++)
        {
            row = table.NewRow();
            row["DictTypeID"] = i;
            row["DictTypeName"] = "DictTypeName " + i;
            table.Rows.Add(row);
        }
        GridView1.DataSource = ds;
        GridView1.DataBind();
    }

    private void MakeDetailTable()
    {
        DataTable table = new DataTable("DetailTable");
        DataColumn column;
        DataRow row;

        column = new DataColumn();
        column.DataType= System.Type.GetType("System.Int32");
        column.ColumnName = "DictItemID";
        column.AutoIncrement = true;//设置该数据列是自增列
        column.Caption = "DictItemID";
        column.ReadOnly = true;
        column.Unique = true;

        table.Columns.Add(column);

        column = new DataColumn();
        column.DataType= System.Type.GetType("System.String");
        column.ColumnName = "DictItemName";
        column.AutoIncrement = false;
        column.Caption = "DictItemName";
        column.ReadOnly = false;
        column.Unique = false;
        table.Columns.Add(column);

        column = new DataColumn();
        column.DataType= System.Type.GetType("System.Int32");
        column.ColumnName = "DictTypeID";
        column.AutoIncrement = false;
        column.Caption = "DictTypeID";
        column.ReadOnly = false;
        column.Unique = false;
        table.Columns.Add(column);

        ds.Tables.Add(table);

        for(int i = 0; i <= 4; i ++)
        {
            row = table.NewRow();
            //row["DictItemID"] = i;//因为该列是自增列所以不用赋值
            row["DictItemName"] = "DictItemName " + i;
            row["DictTypeID"] = 0;
            table.Rows.Add(row);
        }
        for(int i = 0; i <= 4; i ++)
        {
            row = table.NewRow();
            //row["DictItemID"] = i + 5;
            row["DictItemName"] = "DictItemName " + (i+5);
            row["DictTypeID"] = 1;
            table.Rows.Add(row);
        }
    }

    private void MakeDataRelation()
    {
        DataColumn masterColumn =
            ds.Tables["MasterTable"].Columns["DictTypeID"];
        DataColumn detailColumn =
            ds.Tables["DetailTable"].Columns["DictTypeID"];
        //使用DataRelation对象建立主子表的关联关系
        DataRelation relation = new 
            DataRelation("MasterDetail", masterColumn, detailColumn);
        ds.Tables["DetailTable"].ParentRelations.Add(relation);
    }

}

 

posted on 2013-03-18 06:54  松竹梅  阅读(260)  评论(0编辑  收藏  举报