Click here to Skip to main content
14,272,647 members

How to Create TreeView Type GridView

Rate this:
3.19 (13 votes)
Please Sign up or sign in to vote.
3.19 (13 votes)
27 Mar 2008CPOL
How to Create TreeView Type GridView

Download Default7.zip - 2.27 KB

Introduction

1-Step one Create two Table as shown below.

one.jpg

two.jpg

2-Create project and darg and drop a GridView on the web form.

Tree1.jpg

Here is the code for the project

protected void Page_Load(object sender, EventArgs e)

{

if (!IsPostBack)

{

SqlConnection Con = new SqlConnection(Cn);

string sel = "select ParentName,ParentID,Dateof from ParentTable";

SqlCommand cmd = new SqlCommand(sel, Con);

DataTable dt = new DataTable();

dt.Columns.Add(new DataColumn("ParentID", typeof(string)));

dt.Columns.Add(new DataColumn("ChildId", typeof(string)));

dt.Columns.Add(new DataColumn("Name", typeof(string)));

dt.Columns.Add(new DataColumn("Dateof", typeof(string)));

Con.Open();

SqlDataReader reader = cmd.ExecuteReader();

while (reader.Read())

{

DataRow dr = dt.NewRow();

int Pid = Convert.ToInt32(reader["ParentID"]);

dr["ParentID"] = (reader["ParentID"]);

dr["ChildId"] = null;

dr["Name"] = (reader["ParentName"]);

dr["Dateof"] = reader["Dateof"]; 

dt.Rows.Add(dr);

DataTable Mytable = Test(Pid.ToString());

foreach (DataRow dro in Mytable.Rows)

{

DataRow dr2 = dt.NewRow();

dr2["ChildId"] = dro["ChildId"];

dr2["Name"] = dro["ChildName"];

dr2["Dateof"] = dro["Dateof"];

dt.Rows.Add(dr2);

}

 
}

reader.Close();

Con.Close();

GridView1.DataSource = dt;

GridView1.DataBind();

}

}

protected DataTable Test(string ParentId)

{

SqlConnection Con = new SqlConnection(Cn);

DataTable dt = new DataTable();

dt.Columns.Add(new DataColumn("ChildName", typeof(string)));

dt.Columns.Add(new DataColumn("ChildId", typeof(string)));

dt.Columns.Add(new DataColumn("Dateof", typeof(string)));

string sel2 = "select Dateof,ChildName,ChildId from ChildTable where ParentId=" + ParentId;

SqlCommand cmd_2 = new SqlCommand(sel2, Con);

Con.Open();

SqlDataReader reader2 = cmd_2.ExecuteReader();

while (reader2.Read())

{

DataRow dr2 = dt.NewRow();

dr2["ChildName"] = reader2["ChildName"];

dr2["ChildId"] = reader2["ChildId"]; 

dr2["Dateof"] = reader2["Dateof"];

dt.Rows.Add(dr2);

}

return dt;

}

protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)

{

if (e.Row.RowType == DataControlRowType.Header)

{

e.Row.Cells[1].Visible = false;

e.Row.Cells[2].Visible = false;

}

if (e.Row.RowType == DataControlRowType.DataRow)

{

e.Row.Cells[1].Visible = false;

e.Row.Cells[2].Visible = false;

Button MinButton = (Button)e.Row.Cells[0].FindControl("MinBT");

MinButton.CommandArgument = e.Row.RowIndex.ToString();

Button addButton = (Button)e.Row.Cells[0].FindControl("PluseBT");

addButton.CommandArgument = e.Row.RowIndex.ToString();

// e.Row.Cells[3].Visible = false;

}

}

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)

{

if (e.Row.RowType == DataControlRowType.DataRow)

{

string ShowHide=e.Row.Cells[1].Text;

ShowHide = ShowHide.Replace(" ", "");

if (ShowHide.Trim().Length == 0)

{

Button Bt_Min=(Button)e.Row.Cells[0].FindControl("MinBT");

Bt_Min.Visible=false;

Button Bt_plus=(Button)e.Row.Cells[0].FindControl("PluseBT");

Bt_plus.Visible=false;



}

 

}

}

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)

{

if (e.CommandName == "_Show")

{

int index = Convert.ToInt32(e.CommandArgument);

GridViewRow row = GridView1.Rows[index];

int G_Count = GridView1.Rows.Count;

for (int i = index + 1; i < G_Count; i++)

{

if (GridView1.Rows[i].Cells[1].Text == " ")

{

GridView1.Rows[i].Visible = true;

}

else

{

Button Bt_Min = (Button)row.Cells[0].FindControl("MinBT");

Bt_Min.Visible = true;

Button Bt_plus = (Button)row.Cells[0].FindControl("PluseBT");

Bt_plus.Visible = false;

break;

}
Button Bt_Min1 = (Button)row.Cells[0].FindControl("MinBT");

Bt_Min1.Visible = true;

Button Bt_plus1 = (Button)row.Cells[0].FindControl("PluseBT");

Bt_plus1.Visible = false;


}

}

if (e.CommandName == "_Hide")

{

int index = Convert.ToInt32(e.CommandArgument);

GridViewRow row = GridView1.Rows[index];

int G_Count=GridView1.Rows.Count;

for (int i = index + 1; i < G_Count; i++)

{

if (GridView1.Rows[i].Cells[1].Text == " ")

{

GridView1.Rows[i].Visible = false;

}

else

{

Button Bt_Min = (Button)row.Cells[0].FindControl("MinBT");

Bt_Min.Visible = false;

Button Bt_plus = (Button)row.Cells[0].FindControl("PluseBT");

Bt_plus.Visible = true;

break;



}
Button Bt_Min1 = (Button)row.Cells[0].FindControl("MinBT");

Bt_Min1.Visible = false;

Button Bt_plus1 = (Button)row.Cells[0].FindControl("PluseBT");

Bt_plus1.Visible = true;


}



}

Sa1.jpg

Open close the Nodes

Sa2.jpg

Points of Interest

Web Controls

History

Keep a running update of any changes or improvements you've made here.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

T.Ashraf
Chief Technology Officer Compuacademy.net
United States United States
More than 15 years of experience in design, architecture and development of various commercial objects oriented application.Other Specialties

Data Migration:
• MS Access database to SQL 2005/2008
• MS Access database to Oracle
• MS Access database to My SQL
• FoxPro to SQL

Application Migration:
• Converted MS Access application to .net web application (Asp.net)
• Excel Application to .net 3.5 web application
• FoxPro application to .net 3.5
Reporting development and support
• MS access reports
• Crystal reports
• SQL Reports(SSRS)
• DevExpress reports
• Cognos reports
Application development and support
• .net Application web /Win forms
• SharePoint
• MS Access
• Website
• Ecommerce
• WCF
• Web Services
3rd Party Control Support
• DevExpress
• .netForum
• Telerik
Version controls Support
• Team Foundation Server
• Source Safe
• CVS
• SVN

Comments and Discussions

 
QuestionVB Version Pin
AsianRogueOne20-Oct-09 8:20
memberAsianRogueOne20-Oct-09 8:20 
GeneralMy vote of 1 Pin
SomeGuyThatIsMe11-Aug-09 8:17
memberSomeGuyThatIsMe11-Aug-09 8:17 
Generaltnx Pin
mike8421-Jul-09 19:46
membermike8421-Jul-09 19:46 
QuestionHow to Set TreeNode Button's Text ("+" or "-") VerticalAlign Middle? Pin
asatucap17-Jun-09 15:46
memberasatucap17-Jun-09 15:46 
AnswerRe: How to Set TreeNode Button's Text ("+" or "-") VerticalAlign Middle? Pin
T.Ashraf17-Jun-09 15:49
memberT.Ashraf17-Jun-09 15:49 
QuestionComplie Error: Pin
asatucap16-Jun-09 20:06
memberasatucap16-Jun-09 20:06 
AnswerRe: Complie Error: Pin
T.Ashraf17-Jun-09 7:28
memberT.Ashraf17-Jun-09 7:28 
GeneralRe: Complie Error: Pin
asatucap17-Jun-09 15:36
memberasatucap17-Jun-09 15:36 
Thanks!code is working now.
GeneralInherited from the GridView Pin
Jim Brooks28-May-08 11:59
memberJim Brooks28-May-08 11:59 
Generalthe idea is good, but . . . Pin
Luiz_Augusto28-Mar-08 3:25
memberLuiz_Augusto28-Mar-08 3:25 
GeneralRe: the idea is good, but . . . Pin
T.Ashraf28-Mar-08 3:35
memberT.Ashraf28-Mar-08 3:35 
GeneralRe: the idea is good, but . . . Pin
T.Ashraf28-Mar-08 4:10
memberT.Ashraf28-Mar-08 4:10 
QuestionWhere is the solution? Pin
Dewey27-Mar-08 19:40
memberDewey27-Mar-08 19:40 
AnswerRe: Where is the solution? Pin
T.Ashraf28-Mar-08 0:46
memberT.Ashraf28-Mar-08 0:46 
GeneralRe: Where is the solution? Pin
Dewey30-Mar-08 14:56
memberDewey30-Mar-08 14:56 
GeneralRe: Where is the solution? Pin
T.Ashraf31-Mar-08 3:35
memberT.Ashraf31-Mar-08 3:35 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Article
Posted 27 Mar 2008

Stats

136.7K views
4.3K downloads
45 bookmarked