近期遇到了TreeView的数据库绑定问题,确实是弄了我好几天,特别是多级节点的分步绑定,最開始不分步,发现所有载入页面都卡爆了,真心让人头疼。所以放出来,给须要的朋友看看,以免大家走冤枉路。
1.仅仅有一级节点的数据表绑定
部分代码:
protected void treeviewbind()
{
string sqlstr=ConfigurationManager.AppSettings["constr"];
SqlConnection con = new SqlConnection(sqlstr);
con.Open();
string strfac = "select * from tDepartment";
SqlDataAdapter ada = new SqlDataAdapter(strfac,con);
DataTable dt = new DataTable();
ada.Fill(dt);
string id = "Department";
string text = "Department";
Bind_Tv(dt, id,text);
dt.Dispose();
ada.Dispose();
con.Close(); }
protected void Bind_Tv(DataTable dt, string id, string text) //TreeView的递归绑定
{
DataView dv = new DataView(dt);//将DataTable存到DataView中,以便于筛选数据
TreeNode tn;//建立TreeView的节点(TreeNode),以便将取出的数据加入到节点中
foreach (DataRowView row in dv)
{
tn = new TreeNode();//建立一个新节点(学名叫:一个实例)
tn.Value = row[id].ToString();//节点的Value值,一般为数据库的id值
tn.Text = row[text].ToString();//节点的Text,节点的文本显示
TreeView1.Nodes.Add(tn);//将该节点加入到TreeView中
}
}
2.多级节点分步载入绑定
相关代码:
<asp:TreeView ID="TreeView1" runat="server" ImageSet="Simple" CssClass="gridview_m"
OnSelectedNodeChanged="TreeView1_SelectedNodeChanged" NodeIndent="35" ExpandDepth="0"
ShowLines="True" OnTreeNodeExpanded="TreeView1_TreeNodeExpanded">
</asp:TreeView>
protected void bind() //TreeView的数据绑定
{
string sqlstr = ConfigurationManager.AppSettings["constr"];
SqlConnection conn = new SqlConnection(sqlstr);
conn.Open(); string sqlsel = "select * from tDataSheetDirectory";
SqlCommand cmd = new SqlCommand(sqlsel, conn);
SqlDataAdapter sda = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
sda.Fill(dt); string id = "DataSheetDirectoryID";
string text = "DirectoryName";
string pid = "FatherID"; DataView dvtop = new DataView(dt); //加入根节点
TreeNode tntop = new TreeNode();
dvtop.RowFilter = "FatherID is null";
foreach (DataRowView row in dvtop)
{
tntop.Value = row[id].ToString();
tntop.Text = row[text].ToString();
TreeView1.Nodes.Add(tntop);
}
sda.Dispose();
cmd.Dispose();
conn.Close();
Bind_Tv(dt, tntop, tntop.Value, id, pid, text);
}
protected void Bind_Tv(DataTable dt, TreeNode p_Node, string pid_val, string id, string pid, string text) //TreeView的递归绑定
{
DataView dv = new DataView(dt);//将DataTable存到DataView中,以便于筛选数据
//建立TreeView的节点(TreeNode),以便将取出的数据加入到节点中
//下面为三元运算符,假设父id为空,则为构建“父id字段 is null”的查询条件,否则构建“父id字段=父id字段值”的查询条件
string filter = string.IsNullOrEmpty(pid_val) ? pid + " is null" : string.Format(pid + "='{0}'", pid_val);
dv.RowFilter = filter;//利用DataView将数据进行筛选,选出同样 父id值 的数据
foreach (DataRowView row in dv)
{
TreeNode tn = new TreeNode();
tn.Value = row[id].ToString();//节点Value值
tn.Text = row[text].ToString();//节点Text值
p_Node.ChildNodes.Add(tn);//该节点加入到上级节点中
}
} protected void TreeView1_SelectedNodeChanged(object sender, EventArgs e)
{
ViewState["DataSheetState"] = 0;
int directoryid = Convert.ToInt32(this.TreeView1.SelectedNode.Value);
string sqlstr = ConfigurationManager.AppSettings["constr"];
SqlConnection con1 = new SqlConnection(sqlstr);
con1.Open();
string strfac1 = "select * from tDataSheetDirectory where DataSheetDirectoryID='" + directoryid + "'";
SqlCommand cmd1 = new SqlCommand(strfac1, con1);
SqlDataReader dr1 = cmd1.ExecuteReader();
dr1.Read();
if (dr1.HasRows)
{
//Label1.Text = dr1["DirectoryName"].ToString();
}
dr1.Dispose();
cmd1.Dispose();
con1.Close();
//ViewState["DataSheetDirectoryID"] = this.TreeView1.SelectedNode.Value;
//GridViewBind("select * from tDataSheet where FatherDirID='" + this.TreeView1.SelectedNode.Value + "'");
GridViewBind("select tDataSheetDirectory.*,tDataSheet.* from tDataSheetDirectory,tDataSheet where tDataSheet.FatherDirID='" + this.TreeView1.SelectedNode.Value + "' and tDataSheet.FatherDirID=tDataSheetDirectory.DataSheetDirectoryID and tDataSheetDirectory.Hidden='显示'");
for (int i = 0; i < this.TreeView1.Nodes.Count; i++)
{//跌迭根节点
if (this.TreeView1.SelectedValue == this.TreeView1.Nodes[i].Value)
{//假设选中的是根节点,就展开
this.TreeView1.SelectedNode.Expanded = true;
}
else
{//假设选中的不是根节点
for (int j = 0; j < this.TreeView1.SelectedNode.Parent.ChildNodes.Count; j++)
{//就让选中节点的全部同级节点收缩
this.TreeView1.SelectedNode.Parent.ChildNodes[j].CollapseAll();
}
//然后再展开选中的节点及其全部父节点
//this.TreeView1.SelectedNode.Parent.Expanded = true;
this.TreeView1.SelectedNode.Expanded = true;
}
}
}protected void TreeView1_TreeNodeExpanded(object sender, TreeNodeEventArgs e)
{
string sqlstr = ConfigurationManager.AppSettings["constr"];
SqlConnection conn = new SqlConnection(sqlstr);
conn.Open(); TreeNode exnode = e.Node; //展开节点
int fid = Convert.ToInt32(exnode.Value); //展开节点ID string sqlsel = "select * from tDataSheetDirectory";
SqlCommand cmd = new SqlCommand(sqlsel, conn);
SqlDataAdapter sda = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
sda.Fill(dt); NodeExpand(dt, exnode, fid);
} protected void NodeExpand(DataTable dt, TreeNode p_node, int pid)
{
int nextnodec = p_node.ChildNodes.Count; //下级节点个数
for (int i = 0; i < nextnodec; i++)
{
TreeNode tn = p_node.ChildNodes[i];
tn.ChildNodes.Clear();
int nodevalue = Convert.ToInt32(tn.Value);
DataView ndv = new DataView(dt); //获取整个表
ndv.RowFilter = "FatherID = '" + nodevalue + "'"; TreeNode tnn; //下级节点
foreach (DataRowView rown in ndv)
{
tnn = new TreeNode();
tnn.Text = rown["DirectoryName"].ToString();
tnn.Value = rown["DataSheetDirectoryID"].ToString();
tn.ChildNodes.Add(tnn);
}
}
}