ASP.NET生成树形显示的GridView实现思路

目的:生成树形结构的表格数据(EasyUI也有TreeGrid,此处只是提供一个思路),可以扩展单击展开/收缩节点
图例:

类代码:


代码如下:

using System;
using System.Data;
/// <summary>
///GridViewHelper 的摘要说明
/// </summary>
public class GridViewHelper
{
private string gridline; //连接线
private DataTable dt; //传入的DataTable
private DataTable dtOut; //输出的DataTable
private DataRow row; //用于从DataView取数据并增加到dtOut
public GridViewHelper()
{
//
//TODO: 在此处添加构造函数逻辑
//
}
/// <summary>
/// 生成树形结构的DataTable
/// </summary>
/// <param name="datatable">原始数据表</param>
/// <param name="parentField">上级节点关键字段</param>
/// <param name="parentValue">上级节点值</param>
/// <param name="keyField">本节点关键字段</param>
/// <param name="textField">显示的文本字段</param>
/// <param name="sortString">排序字符串</param>
/// <returns>处理后的DataTable</returns>
public DataTable GetGridViewTreeData(DataTable datatable, string parentField, string parentValue, string keyField, string textField, string sortString)
{
datatable.Columns.Add("level", Type.GetType("System.Int32"));
dt = datatable;
dtOut = datatable.Clone();
resetTextField(parentField, parentValue, keyField, textField, sortString, 0);
return dtOut;
}
/// <summary>
/// 递归生成新的节点名称(带连接线)
/// </summary>
/// <param name="parentValueField">上级节点关键字段</param>
/// <param name="parentValue">上级节点值</param>
/// <param name="keyField">本节点关键字段</param>
/// <param name="textField">显示出来的文本字段</param>
/// <param name="sortString">排序字符串</param>
/// <param name="level">树深度</param>
/// <returns></returns>
private void resetTextField(string parentValueField, string parentValue, string keyField, string textField, string sortString,int level)
{
DataView dv = new DataView(dt, parentValueField + "='" + parentValue + "'", sortString, DataViewRowState.CurrentRows);
int a = dv.Count;
if (dv.Count == 0)
{
return;
}
for (int i = 0; i < a; i++)
{
gridline = "";
dv.RowFilter = parentValueField + "='" + parentValue + "'";
dv.Sort = sortString;
getTreeLine(parentValueField, dv[i][parentValueField].ToString(), keyField, dv[i][keyField].ToString(), sortString);
dv.RowFilter = parentValueField + "='" + parentValue + "'";
dv.Sort = sortString;
row = dtOut.NewRow();
for (int c = 0; c < dv[i].Row.ItemArray.Length; c++)
{
row[c] = dv[i][c];
}
dtOut.Rows.Add(row);
dtOut.Rows[dtOut.Rows.Count - 1][textField] = gridline + (i == a - 1 ? "┗" : "┣") + dv[i][textField].ToString();
dtOut.Rows[dtOut.Rows.Count - 1]["level"] = level;
resetTextField(parentValueField, dv[i][keyField].ToString(), keyField, textField, sortString,level+1);
}
dv.Dispose();
}
/// <summary>
/// 回溯生成树的连接线
/// </summary>
/// <param name="parentValueField">上级节点关键字段</param>
/// <param name="parentValue">上级节点值</param>
/// <param name="keyField">本节点关键字段</param>
/// <param name="nodeKey">本节点值</param>
/// <param name="sortString">排序字符串</param>
/// <returns></returns>
private void getTreeLine(string parentValueField, string parentValue, string keyField, string nodeKey, string sortString)
{
//选择父层节点
DataView dv = new DataView(dt, keyField + "='" + parentValue + "'", sortString, DataViewRowState.CurrentRows);
if (dv.Count > 0)
{
//选择父节点同级节点
dv.RowFilter = parentValueField + "='" + dv[0][parentValueField].ToString() + "'";
dv.Sort = sortString;
for (int j = 0; j < dv.Count; j++)
{
if (dv[j][keyField].ToString() == parentValue)
{
if (j == dv.Count - 1)
{
gridline = " " + gridline;
}
else
{
gridline = "┃" + gridline;
}
}
}
getTreeLine(parentValueField, dv[0][parentValueField].ToString(), keyField, dv[0][keyField].ToString(), sortString);
}
dv.Dispose();
}
}

HTML示例代码:


代码如下:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="GridViewTree.aspx.cs" Inherits="GridViewTree" %>
<!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>生成树形显示的GridView</title>
<style type="text/css">
body
{
line-height: 16px;
font-size: 14px;
}
td
{
padding: 0 2px;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<table>
<tr>
<td valign="top">
<asp:DropDownList ID="DropDownList1" runat="server">
</asp:DropDownList>
</td>
<td>
<asp:GridView ID="GridView1" runat="server" BorderWidth="1px" CellPadding="3" GridLines="Vertical"
AutoGenerateColumns="False" BackColor="White" BorderColor="#E7E7FF" BorderStyle="None">
<AlternatingRowStyle BackColor="#F7F7F7" />
<Columns>
<asp:BoundField DataField="ConText" HeaderText="文本" />
<asp:BoundField DataField="id" HeaderText="ID">
<ItemStyle HorizontalAlign="Right" Width="80px" />
</asp:BoundField>
<asp:BoundField DataField="ParentID" HeaderText="父ID">
<ItemStyle HorizontalAlign="Right" Width="80px" />
</asp:BoundField>
<asp:BoundField DataField="level" HeaderText="Level">
<ItemStyle HorizontalAlign="Right" Width="80px" />
</asp:BoundField>
</Columns>
<FooterStyle BackColor="#B5C7DE" ForeColor="#4A3C8C" />
<HeaderStyle BackColor="#4A3C8C" Font-Bold="True" ForeColor="#F7F7F7" />
<PagerStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" HorizontalAlign="Right" />
<RowStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" />
<SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="#F7F7F7" />
<SortedAscendingCellStyle BackColor="#F4F4FD" />
<SortedAscendingHeaderStyle BackColor="#5A4C9D" />
<SortedDescendingCellStyle BackColor="#D8D8F0" />
<SortedDescendingHeaderStyle BackColor="#3E3277" />
</asp:GridView>
</td>
</tr>
</table>
</form>
</body>
</html>

后台代码:


代码如下:

DataTable dt = gvHelper.GetGridViewTreeData(datatable, "ParentID", "1", "ID", "ConText", "ConText asc");
GridView1.DataSource = dt;
GridView1.DataBind();
DropDownList1.Items.Add("");
DropDownList1.AppendDataBoundItems = true;
DropDownList1.DataSource = dt;
DropDownList1.DataTextField = "ConText";
DropDownList1.DataValueField = "ID";
DropDownList1.DataBind();

(0)

相关推荐

  • 常用的在数据库中建立无限级树形菜单的asp.net代码

    复制代码 代码如下: private DataTable GetTable(int topid) { DataTable dt = null; try { string constr = "server=.;database=tqnpc;uid=sa;pwd=sa"; string selstr = "select * from RW_工作关系 where main_id=" + topid + ""; SqlConnection con = n

  • asp.net 获取指定文件夹下所有子目录及文件(树形)

    #region 获取指定文件夹下所有子目录及文件(树形)         /****************************************          * 函数名称:GetFoldAll(string Path)          * 功能说明:获取指定文件夹下所有子目录及文件(树形)          * 参    数:Path:详细路径          * 调用示列:          *           string strDirlist = Server.M

  • asp.net实现DropDownList,TreeView,ListBox的无限极分类目录树

    本文实例讲述了asp.net实现DropDownList,TreeView,ListBox的无限极分类目录树.分享给大家供大家参考,具体如下: #region DropDownList无限递归显示层次关系 /// <summary> /// 创建无限分级下拉列表框 /// </summary> /// <param name="ddlst">下拉控件</param> /// <param name="dt">

  • ASP.NET递归法求阶乘解决思路

    前台: 复制代码 代码如下: <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> !<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="=" /> <asp:TextBox ID="Tex

  • Jquery.TreeView结合ASP.Net和数据库生成菜单导航条

    下面的例子将结合项目实际运用来使用Jquery.TreeView,当然在使用控件树需要现在相应的js文件 下面就把我生成的TreeView展现给大家看看希望对大家有所帮助!在使用之前需要下载控件树的js文件和Css样式 介绍一下表结构 M_ID M_Name M_ParentID M_URL M_Sort 然后新建一个网站,在新建的网站下添加CSS文件夹和js文件夹分别用于存放Css样式和JS,同时再添加一个image文件夹用于存放TreeView的图片 接着我们就开始实现我们的功能了! 前台代

  • Asp.net treeview实现无限级树实现代码

    先看看效果图: 先看看数据库表的设计,数据表主要包括ID,Name,ParentID这三项,其中ID是主键,ParentID对应节点的父节点: 方法一:用递归遍历数据,并将节点逐个添加到treeview中去. 1.先进行数据库连接和数据的读取,并将根节点先添加进treeview中,并利用递归getTreeView()实现数据的遍历和添加: 复制代码 代码如下: protected void Page_Load(object sender, EventArgs e) { if (!Page.IsP

  • asp.net实现递归方法取出菜单并显示在DropDownList中(分栏形式)

    本文实例讲述了asp.net实现递归方法取出菜单并显示在DropDownList中的方法.分享给大家供大家参考,具体如下: 树形栏目展示以DropDownList的形式显示出来. protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { DataTable table = bllSite.GetChannelNames(CurrentUser.ParkID); BindChannelType(table,

  • 递归输出ASP.NET页面所有控件的类型和ID的代码

    写一个方法: 复制代码 代码如下: private void DisplayAllControl(Control control, int step) { foreach (Control ctl in control.Controls) { string s = new string('-', step * 4) + ctl.GetType().Name + "〈" + ctl.ID + "〉"; Response.Write(s + "<br/&

  • asp.net TreeView与XML三步生成列表树

    刚我在做Tree view 绑定时自己摸索了一下,网上有人说TreeView绑定数据源,用什么递归绑定啥的,我不想看了,就自己试着写了一个我是这样做的,如果有什么问题请大神指导,我是菜鸟额..1:页面TreeView 叫tvFunction2:新建 adminFunction.xml 复制代码 代码如下: <?xml version="1.0" encoding="utf-8" ?><functions>  <function name

  • asp.net TreeView递归循环子节点生成树形菜单实例

    本文实例讲述了asp.net TreeView递归循环子节点生成树形菜单的方法.分享给大家供大家参考,具体如下: 这里主要用到递归循环获取子结点 /// <summary> /// 生成根节点 /// </summary> /// <param name="treeview"></param> protected void BindTreeView(long ID, TreeView treeview) { DataTable dt =

随机推荐