asp.net DataView to do an infinite polar classification of another use _ practical skills

Source: Internet
Author: User
Database structure:
Classidid PRIMARY Key
Jobclassname the corresponding type name
ClassName the ID of the corresponding parent class
Usual procedure:

Copy Code code as follows:

private void Display (String parentid, String space)
{
DataTable DT;
String strSQL;
strSQL = "SELECT * from tree Where parentid =" + ParentID + "ORDER by ClassID DESC";
SqlDataAdapter SDA = new SqlDataAdapter (strSQL, Conn);
DataSet ds = new DataSet ();
Sda. Fill (ds, "tree");
DT = ds. Tables["Tree"];
if (dt. Rows.Count > 0)
{
foreach (DataRow dr in Dt. Rows)
{
Stropinion + = space + "<font color=red>[" + dr["Jobclassname"]. ToString () + "<br>";
Display (dr["ClassID"]. ToString (), "" + space,false);
}
}
}

Obviously, this approach is that each parent category has to establish a connection, completely wasting resources
Now remove all the categories at once and use the DataView RowFilter property to do multiple filters
Critical code

Copy Code code as follows:

public partial class Tree_Default:System.Web.UI.Page
{
DataTable dt = null;
protected void Page_Load (object sender, EventArgs e)
{
if (! IsPostBack)
{
Bind (0);
}
}
public void bind (int pid)
{
DataTable dt1 = Bindtree (PID);
foreach (DataRow Dr in Dt1. Rows)
{
int id = convert.toint32 (dr["ClassID"). ToString ());
if (PID = = 0)
Response.Write ("<div style= ' width:100%;float:right;") >Else
Response.Write ("<div style= ' Width:25%;float:left;") > "+dr[" Jobclassname "]. ToString () + "</div>");
Bind (ID);
}
}
Public DataTable bindtree (int pid)
{
if (dt = null)
DT = new data (). Getcatelogs ();
DataView root = dt. DefaultView;
Root. RowFilter = "parentid=" + pid;
return root. ToTable ();
}
}

In this way, there is no need to waste resources.
In fact, this article is a little far-fetched, general classification are rarely made changes, directly with the cache or static processing can be, just thought of the record O (∩_∩) o~.

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.