我有一个填充网格视图的数据集。网格视图必须填充一个数据集和多个查询(或本例中的数据表)。
正如您在代码中看到的那样,我使用了 2 个数据表。这些数据表是从数据库(使用 sql server)中填充的。数据表被添加到一个数据集中。在 ASP 代码中,您可以看到 gridview 中有 3 个标签和 3 个列。前 2 列来自第一个数据表或第一个查询。第二个查询填充最后一列或第三列。
当我运行此代码时,它在数据集中找不到lblThree
。LblThree
在数据集中,但在第二个表中,.NET 不知道我猜。你们知道这个错误的解决方案,所以我可以用一个数据集填充我的gridview吗?
using System;
using System.Collections;
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;
namespace TwoDatatableToDataset
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
loadAll();
}
}
private void loadAll()
{ // STEP ONE Put one sql output in an empty dataset: (works well)
//DataSet ds = new DataSet();
//DataTable dt2 = new DataTable();
//string sql = "select SID1 as lblOne, Charge_Amount as lblTwo from [xxx].[xxxx].[xxxx]";
//SqlConnection Scon = new SqlConnection(ConfigurationManager.ConnectionStrings["testserver"].ConnectionString);
//SqlCommand Scmd = new SqlCommand(sql, Scon);
//Scmd.CommandType = CommandType.Text;
//SqlDataAdapter adp = new SqlDataAdapter(Scmd); // all data in adapter
//Scon.Open();
//adp.Fill(ds); //Adapter in dataset
//GridView1.DataSource = ds; //dataset in gridview
//GridView1.DataBind();
//STEP TWO Make 2 datatables set them in one dataset
DataSet ds = new DataSet();
DataTable dt = new DataTable();
DataTable dt2 = new DataTable();
string sql = "select SID1 as lblOne, Charge_Amount as lblTwo from [xxxx].[xxxx].[xxxx]";
SqlConnection Scon = new SqlConnection(ConfigurationManager.ConnectionStrings["testserver"].ConnectionString);
SqlCommand Scmd = new SqlCommand(sql, Scon);
Scmd.CommandType = CommandType.Text;
SqlDataAdapter adp = new SqlDataAdapter(Scmd); // all data in adapter
Scon.Open();
adp.Fill(dt); //Adapter in dataset
sql = "select Currency as lblThree from [xxx].[dbo].[tbl_xx_NI]";
Scmd = new SqlCommand(sql, Scon);
Scmd.CommandType = CommandType.Text;
adp = new SqlDataAdapter(Scmd); // all data in adapter
adp.Fill(dt2); //Adapter in dataset
ds.Tables.Add(dt); //add data table to data set
ds.Tables.Add(dt2);
GridView1.DataSource = ds; //dataset in gridview
GridView1.DataBind(); // ERROR: A field or property with the name 'lblThree' was not found on the selected data source.
Scon.Close();
}
}
}
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="TwoDatatableToDataset._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>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
AutoGenerateDeleteButton="False" AutoGenerateEditButton="False">
<Columns>
<asp:BoundField DataField="lblOne" HeaderText="One" SortExpression="lblOne">
<ItemStyle Width="20%"/>
</asp:BoundField>
<asp:BoundField DataField="lblTwo" HeaderText="Two" SortExpression="lblTwo">
<ItemStyle Width="20%" />
</asp:BoundField>
<asp:BoundField DataField="lblThree" HeaderText="Three" SortExpression="lblThree">
<ItemStyle Width="20%" />
</asp:BoundField>
</Columns>
</asp:GridView>
</div>
</form>
</body>
</html>