合并两个DataSet的数据内容的方法
default.aspx
<%@ 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" cellpadding="4" forecolor="#333333"
gridlines="none">
<rowstyle backcolor="#eff3fb" />
<footerstyle backcolor="#507cd1" font-bold="true" forecolor="white" />
<pagerstyle backcolor="#2461bf" forecolor="white" horizontalalign="center" />
<selectedrowstyle backcolor="#d1ddf1" font-bold="true" forecolor="#333333" />
<headerstyle backcolor="#507cd1" font-bold="true" forecolor="white" />
<editrowstyle backcolor="#2461bf" />
<alternatingrowstyle backcolor="white" />
</asp:gridview>
</div>
</form>
</body>
</html>
default.aspx.cs
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;
using system.data.sqlclient;
public partial class _default : system.web.ui.page
{
protected void page_load(object sender, eventargs e)
{
dataset dssource = new dataset(); //创建源数据集
dataset dstarget = new dataset(); //创建目标数据集
string constr = configurationmanager.connectionstrings["constr"].tostring();
using (sqlconnection con = new sqlconnection(constr))//创建数据连接
{
//创建数据适配器
sqldataadapter sda = new sqldataadapter("select * from dictionarytype", con);
sda.fill(dssource, "dictionarytype");//将字典类添加到源数据集
sda = new sqldataadapter("select * from dictionaryitem", con);
sda.fill(dstarget, "dictionaryitem");//将字典值添加到目标数据集
}
dstarget.merge(dssource); //将源数据集的dictionarytype表合并到目标数据集中
gridview1.datasource = dstarget.tables["dictionarytype"];
gridview1.databind();
}
}