Student Reviews
( 5 Of 5 )
1 review
Video of Part 163 Dynamically adding treenodes to treeview control in asp net in ASP.net course by kudvenkat channel, video No. 163 free certified online
Text version of the video
http://csharp-video-tutorials.blogspot.com/2013/11/part-163-dynamically-adding-treenodes.html
Healthy diet is very important both for the body and mind. If you like Aarvi Kitchen recipes, please support by sharing, subscribing and liking our YouTube channel. Hope you can help.
https://www.youtube.com/channel/UC7sEwIXM_YfAMyonQCrGfWA/?sub_confirmation1
Slides
http://csharp-video-tutorials.blogspot.com/2014/10/dynamically-adding-treenodes-to.html
All ASP .NET Text Articles
http://csharp-video-tutorials.blogspot.com/p/free-aspnet-video-tutorial.html
All ASP .NET Slides
http://csharp-video-tutorials.blogspot.com/p/aspnet-slides.html
ASP.NET Playlist
https://www.youtube.com/playlist?listPL4cyC4G0M1RQcB4IYS_zwkyBwMyx5AnDM
All Dot Net and SQL Server Tutorials in English
https://www.youtube.com/user/kudvenkat/playlists?view1&sortdd
All Dot Net and SQL Server Tutorials in Arabic
https://www.youtube.com/c/KudvenkatArabic/playlists
Tags
c# treeview add child node to existing node
This is continuation to Part 162. Please watch Part 162, before proceeding.
We want to bind this data to a treeview control. If you need the sql script to create and populate the table with sample data, please refere to Part 162.
Code used in the demo
namespace WebFormsDemo
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
GetTreeViewItems();
}
}
private void GetTreeViewItems()
{
string cs ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
SqlConnection con new SqlConnection(cs);
SqlDataAdapter da new SqlDataAdapter("spGetTreeViewItems", con);
DataSet ds new DataSet();
da.Fill(ds);
ds.Relations.Add("ChildRows", ds.Tables[0].Columns["ID"],
ds.Tables[0].Columns["ParentId"]);
foreach (DataRow level1DataRow in ds.Tables[0].Rows)
{
if (string.IsNullOrEmpty(level1DataRow["ParentId"].ToString()))
{
TreeNode parentTreeNode new TreeNode();
parentTreeNode.Text level1DataRow["TreeViewText"].ToString();
parentTreeNode.NavigateUrl level1DataRow["NavigateURL"].ToString();
GetChildRows(level1DataRow, parentTreeNode);
Treeview1.Nodes.Add(parentTreeNode);
}
}
}
private void GetChildRows(DataRow dataRow, TreeNode treeNode)
{
DataRow[] childRows dataRow.GetChildRows("ChildRows");
foreach (DataRow row in childRows)
{
TreeNode childTreeNode new TreeNode();
childTreeNode.Text row["TreeViewText"].ToString();
childTreeNode.NavigateUrl row["NavigateURL"].ToString();
treeNode.ChildNodes.Add(childTreeNode);
if (row.GetChildRows("ChildRows").Length GREATERTHAN_SYMBOL 0)
{
GetChildRows(row, childTreeNode);
}
}
}
}
}