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)
    {
        DataSet dsSource = new DataSet();      //创建源数据集
        DataTable dt = CreateDataTable();      //创建本地表
        dsSource.Tables.Add(dt);               //将创建的本地表添加到源数据集中

        DataSet copyDataSet1 = dsSource.Copy();//复制整个数据集(包含数据)
        //仅复制源数据集中表数据被更改的部分
        DataSet copyDataSet2 = dsSource.GetChanges();
        //仅复制源数据集中表数据行状态为添加状态的部分
        DataSet copyDataSet3 = dsSource.GetChanges(DataRowState.Added);
        //仅复制源数据集的架构
        DataSet copyDataSet4 = dsSource.Clone();
        //取得源数据集中指定表的指定数据
        DataRow[] copyRows = dsSource.Tables[0].Select("productCode = '0001' ");
        //将取得的数据数组导入到仅复制架构的数据集表中
        DataTable tbSource = copyDataSet4.Tables[0];
        foreach (DataRow copyRow in copyRows)
        {
            tbSource.ImportRow(copyRow);
        }
        GridView1.DataSource = tbSource;
        GridView1.DataBind();
    }

    private DataTable CreateDataTable()
    {
        DataTable dt = new DataTable();
        dt.Columns.Add("productCode", typeof(string));
        dt.Columns.Add("saleDate", typeof(DateTime));
        dt.Columns.Add("saleAmount", typeof(double));
        DataRow dr = dt.NewRow();
        dr["productCode"] = "0001";
        dr["saleDate"] = Convert.ToDateTime("2009-2-1");
        dr["saleAmount"] = 1000;
        dt.Rows.Add(dr);
        dr = dt.NewRow();
        dr["productCode"] = "0001";
        dr["saleDate"] = Convert.ToDateTime("2009-1-1");
        dr["saleAmount"] = 2000;
        dt.Rows.Add(dr);
        dr = dt.NewRow();
        dr["productCode"] = "0002";
        dr["saleDate"] = Convert.ToDateTime("2009-1-1");
        dr["saleAmount"] = 3000;
        dt.Rows.Add(dr);
        return dt;
    }

}

 

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