Click here to Skip to main content
15,884,353 members
Articles / Web Development / ASP.NET
Article

How to Create TreeView Type GridView

Rate me:
Please Sign up or sign in to vote.
3.19/5 (13 votes)
27 Mar 2008CPOL 157.4K   4.5K   45   36
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)


Written By
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

 
QuestionWhat if I wants to add image or ImageButton instead of this Plus/Minus Button? Pin
Member 113632047-Apr-15 16:52
Member 113632047-Apr-15 16:52 
QuestionAdd Multiple child and save at one click. Pin
Abhijit.Kadam0116-Jul-14 3:54
Abhijit.Kadam0116-Jul-14 3:54 
Questioncreating buttons Pin
Member 949372025-Jan-14 20:10
Member 949372025-Jan-14 20:10 
Questionchao ban Pin
hienhoa4-Aug-13 16:47
hienhoa4-Aug-13 16:47 
Questionwith n number of child grids Pin
JinuElsa17-Aug-12 0:01
JinuElsa17-Aug-12 0:01 
AnswerRe: with n number of child grids Pin
T.Ashraf17-Aug-12 2:02
T.Ashraf17-Aug-12 2:02 
GeneralRe: with n number of child grids Pin
JinuElsa17-Aug-12 18:39
JinuElsa17-Aug-12 18:39 
Generalchao ban Pin
hienhoa4-Aug-13 16:47
hienhoa4-Aug-13 16:47 
AnswerRe: with n number of child grids Pin
T.Ashraf27-Sep-12 2:18
T.Ashraf27-Sep-12 2:18 
QuestionNested GridView with multiple childgrdis Pin
dilfizo2-Apr-12 1:57
dilfizo2-Apr-12 1:57 
AnswerRe: Nested GridView with multiple childgrdis Pin
T.Ashraf2-Apr-12 2:59
T.Ashraf2-Apr-12 2:59 
Questionhow to show collapse state initially? Pin
Pragati_v15-Jan-12 21:35
Pragati_v15-Jan-12 21:35 
GeneralThank u very much, I was looking out for this piece of code Pin
n_zaheer_ahmed9-May-11 19:08
n_zaheer_ahmed9-May-11 19:08 
QuestionHow to hide the children initially?? Pin
sanjananarayan2-May-11 23:56
sanjananarayan2-May-11 23:56 
AnswerRe: How to hide the children initially?? Pin
Vladimir Byzgan6-Jun-13 2:20
Vladimir Byzgan6-Jun-13 2:20 
Questiongreat exmple..thx! how to hide children initially? Pin
ajax19713-Mar-11 15:24
ajax19713-Mar-11 15:24 
QuestionCan u any body give some example on dynamic gridview treeview format Pin
dhulipudi5-Jun-10 1:45
dhulipudi5-Jun-10 1:45 
Generalre : hide chlid Pin
jaka konde8-Feb-10 19:06
jaka konde8-Feb-10 19:06 
Generalhide child Pin
jaka konde3-Jan-10 18:48
jaka konde3-Jan-10 18:48 
GeneralRe: hide child Pin
T.Ashraf11-Jan-10 10:05
T.Ashraf11-Jan-10 10:05 
QuestionVB Version Pin
AsianRogueOne20-Oct-09 8:20
AsianRogueOne20-Oct-09 8:20 
GeneralMy vote of 1 Pin
SomeGuyThatIsMe11-Aug-09 8:17
SomeGuyThatIsMe11-Aug-09 8:17 
Generaltnx Pin
mike8421-Jul-09 19:46
mike8421-Jul-09 19:46 
QuestionHow to Set TreeNode Button's Text ("+" or "-") VerticalAlign Middle? Pin
asatucap17-Jun-09 15:46
asatucap17-Jun-09 15:46 
AnswerRe: How to Set TreeNode Button's Text ("+" or "-") VerticalAlign Middle? Pin
T.Ashraf17-Jun-09 15:49
T.Ashraf17-Jun-09 15:49 

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.