aspnet-5337-components-tree-list-examples-how-to-add-new-nodes-in-code-bound-mode.md
This example shows how to add new nodes in code. The ASPxTreeList is bound to a Sql Database.
using System.Data.SqlClient;
protected void Button1_Click(object sender, EventArgs e) {
SqlConnection conn = new SqlConnection();
conn.ConnectionString= "Data Source=(local);Initial Catalog=TestDb;Integrated Security=True";
conn.Open();
int id = GenerateId();
int parentId = 0;
if (ASPxTreeList1.FocusedNode != null)
parentId = (int)ASPxTreeList1.FocusedNode["ID"];
string textCmd = string.Format("INSERT INTO [Table1] ([ID], [ParentID], [Name]) VALUES ({0},
{1}, '{2}')", id, parentId, ASPxTextBox1.Text);
SqlCommand cmd = new SqlCommand(textCmd, conn);
cmd.ExecuteNonQuery();
conn.Close();
ASPxTreeList1.DataBind();
}
<dx:ASPxTreeList ID="ASPxTreeList1" runat="server" AutoGenerateColumns="False"
DataSourceID="SqlDataSource1" KeyFieldName="ID"
ParentFieldName="ParentID" Width="307px">
<Columns>
<dx:TreeListTextColumn FieldName="Name" VisibleIndex="0">
</dx:TreeListTextColumn>
<dx:TreeListSpinEditColumn FieldName="ID" VisibleIndex="1" Visible="False">
<PropertiesSpinEdit DisplayFormatString="g" NumberFormat="Custom">
</PropertiesSpinEdit>
</dx:TreeListSpinEditColumn>
<dx:TreeListSpinEditColumn FieldName="ParentID" VisibleIndex="1" Visible="False">
<PropertiesSpinEdit DisplayFormatString="g" NumberFormat="Custom">
</PropertiesSpinEdit>
</dx:TreeListSpinEditColumn>
</Columns>
<SettingsBehavior AllowFocusedNode="True" />
</dx:ASPxTreeList>
The animation below shows the result: