Click here to Skip to main content
6,935,055 members and growing! (14,421 online)
Email Password   helpLost your password?
Web Development » ASP.NET » Reporting     Beginner License: The Code Project Open License (CPOL)

How to Create TreeView Type GridView

By T.Ashraf

How to Create TreeView Type GridView
C# (C#2.0), Windows (WinXP, Vista), ASP.NET, Ajax
Posted:27 Mar 2008
Views:26,451
Bookmarked:30 times
Unedited contribution
printPrint Friendly   add Share
      Discuss Discuss   Broken Article?Report  
9 votes for this article.
Popularity: 3.00 Rating: 3.14 out of 5
3 votes, 33.3%
1
1 vote, 11.1%
2
1 vote, 11.1%
3

4
4 votes, 44.4%
5

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)

About the Author

T.Ashraf


Member
More than 9 years of experience in programming, design, architecture and development of various commercial object oriented application using C#, Visual Basic.net, ASP.net, relational database (SQL ,Oracle, MYSQL) in Window NT/2000/XP and UNIX environment. Experienced in client-server application development, data security management, third party tools and also has very good exposure to the entire Software Development Life Cycle viz. Requirements Collection and other Project Management activities.
WWW.TANVTECH.COM
www.cheaplimotaxi.com
Occupation: Software Developer (Senior)
Company: TANVTECH
Location: United States United States

Other popular ASP.NET articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 19 of 19 (Total in Forum: 19) (Refresh)FirstPrevNext
Generalre : hide chlid Pinmemberjaka konde20:06 8 Feb '10  
Generalhide child Pinmemberjaka konde19:48 3 Jan '10  
GeneralRe: hide child PinmemberT.Ashraf11:05 11 Jan '10  
QuestionVB Version PinmemberMember 43544459:20 20 Oct '09  
GeneralMy vote of 1 PinmemberSomeGuyThatIsMe9:17 11 Aug '09  
Generaltnx Pinmembermike8420:46 21 Jul '09  
QuestionHow to Set TreeNode Button's Text ("+" or "-") VerticalAlign Middle? Pinmemberasatucap16:46 17 Jun '09  
AnswerRe: How to Set TreeNode Button's Text ("+" or "-") VerticalAlign Middle? PinmemberT.Ashraf16:49 17 Jun '09  
QuestionComplie Error: Pinmemberasatucap21:06 16 Jun '09  
AnswerRe: Complie Error: PinmemberT.Ashraf8:28 17 Jun '09  
GeneralRe: Complie Error: Pinmemberasatucap16:36 17 Jun '09  
GeneralInherited from the GridView PinmemberJim Brooks12:59 28 May '08  
Generalthe idea is good, but . . . PinmemberLuiz_Augusto4:25 28 Mar '08  
GeneralRe: the idea is good, but . . . PinmemberT.Ashraf4:35 28 Mar '08  
GeneralRe: the idea is good, but . . . PinmemberT.Ashraf5:10 28 Mar '08  
GeneralWhere is the solution? PinmemberDewey20:40 27 Mar '08  
GeneralRe: Where is the solution? PinmemberT.Ashraf1:46 28 Mar '08  
GeneralRe: Where is the solution? PinmemberDewey15:56 30 Mar '08  
GeneralRe: Where is the solution? PinmemberT.Ashraf4:35 31 Mar '08  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

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

PermaLink | Privacy | Terms of Use
Last Updated: 27 Mar 2008
Editor:
Copyright 2008 by T.Ashraf
Everything else Copyright © CodeProject, 1999-2010
Web18 | Advertise on the Code Project