45fan.com - 路饭网

搜索: 您的位置主页 > 网络频道 > 阅读资讯:怎么样通过DataGrid嵌套实现树型结构?

怎么样通过DataGrid嵌套实现树型结构?

2016-09-02 09:12:50 来源:www.45fan.com 【

怎么样通过DataGrid嵌套实现树型结构?

怎么样通过DataGrid嵌套实现树型结构?
<%@ Page language="c#" Codebehind="DataGridMasterForm.aspx.cs" AutoEventWireup="false" Inherits="Webs.DataGridMasterForm" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>DataGridMasterForm</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio 7.0">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="DataGridMasterForm" method="post" runat="server">
<h2>使用DataGrid控件实现以主细表显示数据</h2>
<asp:DataGrid id="dgMaster" style="Z-INDEX: 101; LEFT: 48px; POSITION: absolute; TOP: 64px" runat="server" AutoGenerateColumns="False" ShowHeader="False" Width="368px">
<Columns>
<asp:TemplateColumn>
<ItemTemplate>
<TABLE cellSpacing="0" cellPadding="0" width="100%" border="0">
<TR>
<TD bgColor="#3399ff"><FONT color="#ffffff"><B>根目录:
<%# DataBinder.Eval(Container.DataItem, "ParentId") %>
</B></FONT>
</TD>
</TR>
<TR>
<TD align="right">
<asp:DataGrid id=dgDetail runat="server" AutoGenerateColumns="False" DataSource='<%# ((System.Data.DataRowView)Container.DataItem).CreateChildView("tableRelation") %>' DataKeyField="Id" BorderColor="#336666" BorderStyle="Double" BorderWidth="3px" BackColor="White" CellPadding="4" GridLines="Horizontal" Width="100%" Font-Size="Smaller">
<SelectedItemStyle Font-Bold="True" ForeColor="White" BackColor="#339966"></SelectedItemStyle>
<ItemStyle ForeColor="#333333" BackColor="White"></ItemStyle>
<HeaderStyle Font-Bold="True" ForeColor="White" BackColor="#336666"></HeaderStyle>
<FooterStyle ForeColor="#333333" BackColor="White"></FooterStyle>
<Columns>

<asp:BoundColumn DataField="Id" HeaderText="ID"></asp:BoundColumn>
<asp:BoundColumn DataField="DirectionName" HeaderText="子目录"></asp:BoundColumn>
</Columns>
<PagerStyle HorizontalAlign="Center" ForeColor="White" BackColor="#336666" Mode="NumericPages"></PagerStyle>
</asp:DataGrid></TD>
</TR>
</TABLE>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid>
</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;

namespace Webs
{
/// <summary>
/// DataGridMasterForm 的摘要说明。
/// </summary>
public class DataGridMasterForm : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataGrid dgMaster;
protected System.Web.UI.WebControls.DataGrid dgDetail;

private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
if(!IsPostBack)
{
DataGridBind();
}
}
private void DataGridBind()
{
//定义数据连接对象,其中数据库连接字符串是在Web.Config文件中定义的
SqlConnection conn = new SqlConnection(ConfigurationSettings.AppSettings["connStr"].ToString());
//创建数据适配器对象
SqlDataAdapter da = new SqlDataAdapter("SELECT * FROM s0; SELECT * FROM s1",conn);
//创建DataSet对象
DataSet ds = new DataSet();
try
{
//填充数据集
da.Fill(ds);
//设定表名字
ds.Tables[0].TableName = "s0";
ds.Tables[1].TableName = "s1";
DataColumn Parent = ds.Tables["s0"].Columns["Id"];
DataColumn Child = ds.Tables["s1"].Columns["ParentId"];
DataRelation tableRelation = new DataRelation("tableRelation", Parent, Child, false);
ds.Relations.Add(tableRelation);
//进行数据绑定
dgMaster.DataSource = ds.Tables["s0"].DefaultView;
dgMaster.DataBind();
}
catch(Exception error)
{
Response.Write(error.ToString());
}

}

#region Web Form Designer generated code
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);

}
#endregion
}
}
 

本文地址:http://www.45fan.com/a/question/71175.html
Tags: 实现 嵌套 DataGrid
编辑:路饭网
关于我们 | 联系我们 | 友情链接 | 网站地图 | Sitemap | App | 返回顶部