本文主要介紹關於的知識點,對【TreeView的綁定】和【unity TreeView】有興趣的朋友可以看下由【我本良人】投稿的技術文章,希望該技術和經驗能幫到你解決你所遇的【asp.net】相關技術問題。
??????? 最近遇到了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.多級節點分步加載綁定
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);
}
}
}
本文《TreeView的綁定》版權歸我本良人所有,引用TreeView的綁定需遵循CC 4.0 BY-SA版權協議。
本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。