This is my code for treeview from database... I want when I click on parent node to show rows with Id- from 0 to 9, when i click on first child for example 0, Id from 00 to 09, on child 1 Id from 10 to 19....When I click on second child for example 00 to show id from 000 to 009 ......from the same datatable from database.
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
GetTreeViewItems();
}
}
private void GetTreeViewItems()
{
string cs = ConfigurationManager.ConnectionStrings["RegistrationConnectionString"].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["ШифраНаКонто"], 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["ШифраНаКонто"].ToString() + level1DataRow["НазивКонто"].ToString();
GetChildNodes(level1DataRow, parentTreeNode);
TreeView1.Nodes.Add(parentTreeNode);
}
}
}
private void GetChildNodes(DataRow dataRow, TreeNode treeNode)
{
DataRow[] childRows = dataRow.GetChildRows("ChildRows");
foreach (DataRow childRow in childRows)
{
TreeNode childTreeNode = new TreeNode();
childTreeNode.Text = childRow["ШифраНаКонто"].ToString() + childRow["НазивКонто"].ToString();
treeNode.ChildNodes.Add(childTreeNode);
if (childRow.GetChildRows("ChildRows").Length > 0)
{
GetChildNodes(childRow, childTreeNode);
}
}
}
protected void TreeView1_SelectedNodeChanged(object sender, EventArgs e)
{
Loadgr();
}
void Loadgr()
{
string cs = ConfigurationManager.ConnectionStrings["RegistrationConnectionString"].ConnectionString;
SqlConnection con = new SqlConnection(cs);
SqlCommand com = new SqlCommand("select * from КонтенПлан where ШифраНаКонто = @ШифраНаКонто", con);
SqlDataAdapter adap = new SqlDataAdapter(com);
com.Parameters.Add("@ШифраНаКонто", SqlDbType.Int).Value = this.TreeView1.SelectedNode.Text;
DataTable tab = new DataTable();
adap.Fill(tab);
GridView1.DataSource = tab;
GridView1.DataBind();
}
protected void txtnaziv_TextChanged(object sender, EventArgs e)
{
TextBox tsifra = (TextBox)GridView1.FooterRow.Cells[0].FindControl("txtsifra");
TextBox tnaziv = (TextBox)GridView1.FooterRow.Cells[1].FindControl("txtnaziv");
TextBox sifra = (TextBox)GridView1.FindControl("lblsifra");
TextBox naziv = (TextBox)GridView1.FindControl("lblnaziv");
tnaziv.TextChanged += new EventHandler(txtnaziv_TextChanged);
string cs = ConfigurationManager.ConnectionStrings["RegistrationConnectionString"].ConnectionString;
SqlConnection con = new SqlConnection(cs);
con.Open();
SqlCommand cmd = new SqlCommand("insert into КонтенПлан(ШифраНаКонто,НазивКонто) values('" + tsifra.Text + "','" + tnaziv.Text + "')", con);
int result = cmd.ExecuteNonQuery();
SqlDataAdapter ad = new SqlDataAdapter("SELECT * FROM КонтенПлан Where ШифраНаКонто BETWEEN 0 AND 9", con);
DataSet ds = new DataSet();
ad.Fill(ds, "КонтенПлан");
GridView1.DataSource = ds;
con.Close();
if (result == 1)
{
GridView1.DataBind();
}
cmd.Connection = con;
cmd.CommandText = "Update КонтенПлан set ШифраНаКонто='" + sifra.Text + "',НазивКонто='" + naziv.Text + "' where ШифраНаКонто='" + sifra.Text + "'";
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
string cs = ConfigurationManager.ConnectionStrings["RegistrationConnectionString"].ConnectionString;
SqlConnection con = new SqlConnection(cs);
con.Open();
con.Close();
GridView1.EditIndex = -1;
}
}
[edit by="nirav prabtani"]
Set language codeblock and remove extra spaces
[/edit]