Click here to Skip to main content
Click here to Skip to main content
Go to top

How to Create TreeView Type GridView

, 27 Mar 2008
Rate this:
Please Sign up or sign in to vote.
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
Technical Lead TANVTECH
United States United States
More than 13 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

 
QuestionAdd Multiple child and save at one click. PinmemberMember 940671316-Jul-14 3:54 
Questioncreating buttons PinmemberMember 949372025-Jan-14 20:10 
Questionchao ban Pinmemberhienhoa4-Aug-13 16:47 
Questionwith n number of child grids PinmemberJinuElsa17-Aug-12 0:01 
AnswerRe: with n number of child grids PinmemberT.Ashraf17-Aug-12 2:02 
GeneralRe: with n number of child grids PinmemberJinuElsa17-Aug-12 18:39 
Generalchao ban Pinmemberhienhoa4-Aug-13 16:47 
AnswerRe: with n number of child grids PinmemberT.Ashraf27-Sep-12 2:18 
QuestionNested GridView with multiple childgrdis Pinmemberdilfizo2-Apr-12 1:57 
AnswerRe: Nested GridView with multiple childgrdis PinmemberT.Ashraf2-Apr-12 2:59 
Questionhow to show collapse state initially? [modified] PinmemberPragati_v15-Jan-12 21:35 
GeneralThank u very much, I was looking out for this piece of code Pinmembern_zaheer_ahmed9-May-11 19:08 
QuestionHow to hide the children initially?? Pinmembersanjananarayan2-May-11 23:56 
AnswerRe: How to hide the children initially?? PinmemberVladimir Byzgan6-Jun-13 2:20 
Questiongreat exmple..thx! how to hide children initially? Pinmemberajax19713-Mar-11 15:24 
QuestionCan u any body give some example on dynamic gridview treeview format Pinmemberdhulipudi5-Jun-10 1:45 
Generalre : hide chlid Pinmemberjaka konde8-Feb-10 19:06 
Generalhide child Pinmemberjaka konde3-Jan-10 18:48 
GeneralRe: hide child PinmemberT.Ashraf11-Jan-10 10:05 
QuestionVB Version PinmemberMember 435444520-Oct-09 8:20 
GeneralMy vote of 1 PinmemberSomeGuyThatIsMe11-Aug-09 8:17 
Generaltnx Pinmembermike8421-Jul-09 19:46 
QuestionHow to Set TreeNode Button's Text ("+" or "-") VerticalAlign Middle? Pinmemberasatucap17-Jun-09 15:46 
AnswerRe: How to Set TreeNode Button's Text ("+" or "-") VerticalAlign Middle? PinmemberT.Ashraf17-Jun-09 15:49 
QuestionComplie Error: Pinmemberasatucap16-Jun-09 20:06 

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

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

| Advertise | Privacy | Mobile
Web03 | 2.8.140921.1 | Last Updated 27 Mar 2008
Article Copyright 2008 by T.Ashraf
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid