我用递归绑定数据库的一张表
private void Form1_Load(object sender, EventArgs e)
{
fenlei(0, null);
}
private void fenlei(int parentid, TreeNode tn)
{
SqlCommand sqlcom = new SqlCommand();
sqlcom.Connection = sqlcon;
sqlcom.CommandText = "select ID,modulename,parentid,seqno from tree where parentid>=0 order by seqno";
SqlDataAdapter da = new SqlDataAdapter(sqlcom);
dt = new DataTable();
da.Fill(dt);
DataView dvtree = new DataView(dt);
dvtree.RowFilter = "[parentid]=" + parentid;
foreach (DataRowView row in dvtree)
{
if (tn == null)
{
TreeNode node = treeView1.Nodes.Add(row["modulename"].ToString());
node.Tag = row["id"];
fenlei(Int32.Parse(row["id"].ToString()), node);
}
else
{
TreeNode node = tn.Nodes.Add(row["modulename"].ToString());
node.Tag = row["id"];
fenlei(Int32.Parse(row["id"].ToString()), node);
}
}
treeView1.ExpandAll();
}
这是递归的代码 ,数据库中表有3列相关的。 ID列,parentid列(名称),parentid列(父节点的ID是子节点的parentid,parentid相同的放在同一个父节点下)
现在拖动节点后改变了parentid但是必须是拖动一次更新一下才行,
sqlcon.Open();
string strc = "update tree set parentid=" + id + " where id=" + Convert.ToDouble(this.textBox1.Text) + "";
SqlCommand sqlcom = new SqlCommand(strc, sqlcon);
int i = sqlcom.ExecuteNonQuery();
if (i > 0)
{
MessageBox.Show("更改成功");
}
这是拖动一次就要保存的代码。如何把所有要拖动的节点拖动完之后再用递归全部更新数据库?要用递归一次全部更新。。分不多,解决了再加分!!