Click here to Skip to main content
15,891,951 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 158K   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

 
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 
QuestionComplie Error: Pin
asatucap16-Jun-09 20:06
asatucap16-Jun-09 20:06 
AnswerRe: Complie Error: Pin
T.Ashraf17-Jun-09 7:28
T.Ashraf17-Jun-09 7:28 
GeneralRe: Complie Error: Pin
asatucap17-Jun-09 15:36
asatucap17-Jun-09 15:36 
Thanks!code is working now.
GeneralInherited from the GridView Pin
Jim Brooks28-May-08 11:59
Jim Brooks28-May-08 11:59 
Generalthe idea is good, but . . . Pin
Luiz_Augusto28-Mar-08 3:25
Luiz_Augusto28-Mar-08 3:25 
GeneralRe: the idea is good, but . . . Pin
T.Ashraf28-Mar-08 3:35
T.Ashraf28-Mar-08 3:35 
GeneralRe: the idea is good, but . . . Pin
T.Ashraf28-Mar-08 4:10
T.Ashraf28-Mar-08 4:10 
QuestionWhere is the solution? Pin
Dewey27-Mar-08 19:40
Dewey27-Mar-08 19:40 
AnswerRe: Where is the solution? Pin
T.Ashraf28-Mar-08 0:46
T.Ashraf28-Mar-08 0:46 
GeneralRe: Where is the solution? Pin
Dewey30-Mar-08 14:56
Dewey30-Mar-08 14:56 
GeneralRe: Where is the solution? Pin
T.Ashraf31-Mar-08 3:35
T.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.