DataSet 里有两个表
tblExcel,tblStudenttblExcel里有Xh,Score,
tblStudent里有Xh,Xm,Class,我想把其中的一个表绑定到DataGrid上,显示学号,姓名,班级,学分。如何做到???((用relation?))((在线wait))
tblExcel,tblStudenttblExcel里有Xh,Score,
tblStudent里有Xh,Xm,Class,我想把其中的一个表绑定到DataGrid上,显示学号,姓名,班级,学分。如何做到???((用relation?))((在线wait))
<%@ Page language="c#" Codebehind="HierarchicalDataGrid.aspx.cs" AutoEventWireup="false" Inherits="testasp.HierarchicalDataGrid" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<title>显示主次关系数据的例子(点击可展开)</title>
<meta name="GENERATOR" content="Microsoft Visual Studio.NET 7.0">
<meta name="CODE_LANGUAGE" content="Visual Basic 7.0">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<script language="javascript">
function showlist(showid)
{
if (showid.style.display=='none')
{
showid.style.display = '';
}
else
{
showid.style.display = 'none';
}
}
</script>
<body>
<form id="FrmDataGrid" method="post" runat="server">
<P align="center">
<asp:DataGrid id="DataGrid1" BorderColor="#0099FF" runat="server" ShowHeader="False" Width="470px"
CellPadding="0" CellSpacing="0" AutoGenerateColumns="False" BorderWidth="2px" AllowPaging="True">
<Columns>
<asp:TemplateColumn>
<ItemTemplate>
<TABLE cellSpacing="0" cellPadding="0" width="100%" border="0">
<TR>
<TD onclick="showlist(dd<%# DataBinder.Eval(Container.DataItem, "OrderID") %>);" bgColor="#FF6600"><B>订单号:
<%# DataBinder.Eval(Container.DataItem, "OrderID") %>
</B>
</TD>
</TR>
<TR>
<TD align="right" style="DISPLAY: none" id="dd<%# DataBinder.Eval(Container.DataItem, "OrderID") %>">
<asp:DataGrid id=DataGrid2 runat="server" AutoGenerateColumns="False" BorderColor="#33FF33" DataKeyField="OrderID" DataSource='<%# ((DataRowView)(Container.DataItem)).CreateChildView("OrderRelation") %>'>
<HeaderStyle Font-Bold="True" ForeColor="#CC0066" BackColor="#FFCCFF" ></HeaderStyle>
<Columns>
<asp:BoundColumn Visible="False" DataField="OrderID" ReadOnly="True"></asp:BoundColumn>
<asp:TemplateColumn HeaderText="产品名称">
<HeaderStyle Width="300px"></HeaderStyle>
<ItemTemplate>
<%# DataBinder.Eval(Container.DataItem, "ProductName") %>
</ItemTemplate>
</asp:TemplateColumn>
<asp:BoundColumn DataField="UnitPrice" HeaderText="单价"></asp:BoundColumn>
<asp:BoundColumn DataField="Quantity" HeaderText="数量"></asp:BoundColumn>
<asp:BoundColumn DataField="Discount" HeaderText="折扣"></asp:BoundColumn>
</Columns>
</asp:DataGrid>
</TD>
</TR>
</TABLE>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid></P>
</form>
</body>
</HTML>
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections.Specialized;namespace testasp
{
/// <summary>
/// HierarchicalDataGrid 的摘要说明。
/// </summary>
public class HierarchicalDataGrid : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataGrid DataGrid1;
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
if(!Page.IsPostBack)
{
BindGrid();
}
}
private void BindGrid()
{
string BindStr="Select * from Orders; select OrderID ,Products.ProductName,[order Details].Unitprice,[order Details].Quantity,[order Details].discount from [order Details],Products where [Order Details].ProductId=Products.ProductID";
SqlConnection conn =new SqlConnection("server=localhost;Initial Catalog=northwind;Data Source=;User Id=sa;Password=;");
SqlDataAdapter OrderAdapter= new SqlDataAdapter(BindStr, conn); DataSet OrderDataSet = new DataSet();
OrderAdapter.Fill(OrderDataSet);
OrderDataSet.Tables[0].TableName = "Orders";
OrderDataSet.Tables[1].TableName = "Order Details";
DataColumn Parent =new DataColumn("Parent");
DataColumn Child =new DataColumn("Child");
Parent=OrderDataSet.Tables["Orders"].Columns["OrderID"];
Child=OrderDataSet.Tables["Order Details"].Columns["OrderID"];
DataRelation OrderRelation =new DataRelation("OrderRelation", Parent, Child, false);
OrderDataSet.Relations.Add(OrderRelation);
conn.Close();
DataGrid1.DataSource = OrderDataSet.Tables["Orders"].DefaultView;
DataGrid1.DataBind();
}
private void DataGrid1_PageIndexChanged(object source,DataGridPageChangedEventArgs e)
{
this.DataGrid1.CurrentPageIndex=e.NewPageIndex;
DataGrid1.EditItemIndex = -1;
BindGrid();
}
private void DataGrid1_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
e.Item.Cells[0].BackColor = System.Drawing.Color.Ivory;
}
#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
this.DataGrid1.PageIndexChanged += new System.Web.UI.WebControls.DataGridPageChangedEventHandler(this.DataGrid1_PageIndexChanged);
}
#endregion
}
}
DataGrid dd = new DataGrid();
dd.DataSource = ds.Tables["tableName"].DefaultView;
(new DataGrid()).DataSource = ds.Tables["tblStudent"].DefaultView;
tblExcel,tblStudenttblExcel里有Xh,Score,
tblStudent里有Xh,Xm,Class,我想把其中的一个表绑定到DataGrid上,显示学号,姓名,班级,学分。你这个最好是在构造sql查询的时候来做,然后放到一个dataset中去,比如,你可以这样
str="select tblStudent.xh,tblStudent.xm,tblStudentc.class,tblExcel.score from tblStudent left join tblExcel on tblExcel.xh=tblStudent.xh";
这样就方便了
剩下的工作你自己处理吧
一个是来自sql数据库,不知道我说明白没???
DataTable myTable = new DataTable();
DataRow dr = this.myTable.NewRow();
for(……)
{
this.myTable.Rows.Add(dr);
this.myTable.Rows[i]["学号"]=yourvalue;
this.myTable.Rows[i]["姓名"]=yourvalue;
this.myTable.Rows[i]["班级"]=yourvalue;
}
yourrvalue即为你dataset里表中的数据
如 ds.Tables["tblStudent"].Row[i]["Xh"]
行 309:
ds.Relations.Add("ExcelStudent",tblStudent.Columns["StudentID"],tblExcel.Columns["xh"]);可能是tblStudent中StudentID列是string但是tblExcel中xh例是int能强行改变列的数据格式么???
tblExcel.Columns.Add("xm",typeof(string),"Parent(ExcelStudent).StudentName");
tblExcel.Columns.Add("class",typeof(string),"Parent(ExcelStudent).Class");这样的话好像就可以用
myDataGrid.DataSource= tblExcel.DefaultView
来显示那些列的数据了。但上面的那个数据模式问题,,,真受不了!!!!
private void InitTable()
{
DataColumn dc = new DataColumn("学号");
myTable.Columns.Add(dc);
dc = new DataColumn("姓名");
myTable.Columns.Add(dc);
dc = new DataColumn("班级");
myTable.Columns.Add(dc);
}
DataTable dt=new DataTable();
dt.Columns.Add(new DataColumn("xh", typeof(System.String)));
dt.Columns.Add(new DataColumn("xm", typeof(System.String)));
dt.Columns.Add(new DataColumn("Class", typeof(System.String)));
dt.Columns.Add(new DataColumn("score", typeof(System.String)));
DataRow dr;
for(int i=0;i<iRowsCount;i++)
{
DataRow dr1=ds.Tables["tblExcel"].Rows[i];
DataRow dr2=ds.Tables["tblStudent"].Rows[i];
dr=dt.NewRow();
dr["xh"]=dr1["xh"];
dr["xm"]=dr2["xm"];
dr["Class"]=dr2["Class"];
dr["score"]=dr1["score"];
dt.Rows.Add(dr);
}
DataGrid1.DataSource=dt;
DataGrid1.DataBind();
select a.xh,a.Xm,a.Class,b.score from tblStudent a,tblExcel b where a.xh=b.xh
datagrid的绑定就用
DataBinder.Eval(Container.DataItem, "xh")
DataBinder.Eval(Container.DataItem, "xm")
DataBinder.Eval(Container.DataItem, "class")
DataBinder.Eval(Container.DataItem, "score")
他DataSet的2个表一个来自Excel文件,另一个是来自sql数据库
正如 回复人: soft_biao(巴不豆) 所说:他DataSet的2个表一个来自Excel文件,另一个是来自sql数据库----------------------------------------------2/我的问题已经解决方法是 回复人: solsolsol(秋水萧萧) 的,我整理了一下:excelDa.Fill(ds,"excel");
DataTable tblExcel = ds.Tables["excel"];
DataTable tblStudent = ds.Tables["student"];DataView vueS = new DataView(tblStudent);
vueS.Sort = "StudentID";//想用DataView.Find();方法必须指定DataView.Sort;
int intIndex;
DataRowView rowS;DataTable tblPreview = new DataTable();tblPreview.Columns.Add("No",typeof(int));
tblPreview.Columns.Add("Xh",typeof(string));
tblPreview.Columns.Add("Xm",typeof(string));
tblPreview.Columns.Add("Class",typeof(string));
int i=0;
foreach(DataRow row in tblExcel.Rows)
{
intIndex = vueS.Find(Convert.ToString(row["xh"]));
rowS = vueS[intIndex]; DataRow dr = tblPreview.NewRow();
tblPreview.Rows.Add(dr); tblPreview.Rows[i]["No"] = i+1;
tblPreview.Rows[i]["Xh"] =Convert.ToString(row["xh"]);
tblPreview.Rows[i]["Xm"] = rowS["StudentName"];
tblPreview.Rows[i]["Class"] = rowS["Class"]; i++;
}
myDataGrid.DataSource = tblPreview;
myDataGrid.DataBind();
----------------------------------------------
3/我在想下面的这个方法也应该可以:
可以用一个DataRelation和一个苦于中表达式的列模拟联接!ds.Relations.Add("ExcelStudent",tblStudent.Columns["StudentID"],tblExcel.Columns["xh"]);
tblExcel.Columns.Add("xm",typeof(string),"Parent(ExcelStudent).StudentName");
tblExcel.Columns.Add("class",typeof(string),"Parent(ExcelStudent).Class");有兴趣的可以试试,(我继续往下做其它的功能,同时关注此贴)----------------------------------------------4/对了,感谢各位大侠的参予,就100分,我就给solsolsol(秋水萧萧)好了,谢谢。