Asp. NET to generate a tree-shaped display of the GridView implementation of the idea _ practical skills

Source: Internet
Author: User
Objective: To generate tabular data for the tree structure (Easyui also has treegrid, here is just a thought) to extend the click expand/Shrink Node
Legend:

Class Code:
Copy Code code as follows:

Using System;
Using System.Data;
<summary>
Summary description of Gridviewhelper
</summary>
public class Gridviewhelper
{
private string gridline; Connecting lines
Private DataTable DT; Incoming DataTable
Private DataTable dtout; The Output DataTable
Private DataRow Row; Used to fetch data from DataView and increase it to dtout
Public Gridviewhelper ()
{
//
TODO: Add constructor logic here
//
}
<summary>
A DataTable that produces a tree structure
</summary>
<param name= "DataTable" > Raw datasheet </param>
<param name= "ParentField" > Parent node key field </param>
<param name= "Parentvalue" > Parent node Value </param>
<param name= "Keyfield" > This node key field </param>
<param name= "TextField" > Display text fields </param>
<param name= "sortstring" > Sort string </param>
The datatable</returns> after <returns> treatment
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>
Recursive generation of new node names (with connecting wiring)
</summary>
<param name= "Parentvaluefield" > Parent node key field </param>
<param name= "Parentvalue" > Parent node Value </param>
<param name= "Keyfield" > This node key field </param>
<param name= "TextField" > Display text fields </param>
<param name= "sortstring" > Sort string </param>
<param name= "Level" > Tree depth </param>
<returns></returns>
private void Resettextfield (String Parentvaluefield, String parentvalue, String Keyfield, String TextField, String sortst Ring,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? "┗": "Out") + 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>
A connection line that backtracking spanning a tree
</summary>
<param name= "Parentvaluefield" > Parent node key field </param>
<param name= "Parentvalue" > Parent node Value </param>
<param name= "Keyfield" > This node key field </param>
<param name= "Nodekey" > This node value </param>
<param name= "sortstring" > Sort string </param>
<returns></returns>
private void Gettreeline (String Parentvaluefield, String parentvalue, String Keyfield, String Nodekey, String sortstring)
{
Select Parent Layer Node
DataView dv = new DataView (DT, Keyfield + "= '" + Parentvalue + "" ", Sortstring, DataViewRowState.CurrentRows);
if (DV. Count > 0)
{
Select parent node Sibling node
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 Sample code:
Copy Code code as follows:

<%@ 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 ">
<title> generating a tree-shaped display of gridview</title>
<style type= "Text/css" >
Body
{
line-height:16px;
font-size:14px;
}
Td
{
padding:0 2px;
}
</style>
<body>
<form id= "Form1" runat= "Server" >
<table>
<tr>
&LT;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= "text"/>
<asp:boundfield datafield= "id" headertext= "id" >
<itemstyle horizontalalign= "Right" width= "80px"/>
</asp:BoundField>
<asp:boundfield datafield= "ParentID" headertext= "Parent 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"/>
<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>

Background code:
Copy Code code as follows:

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 ();

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.