C#中TreeView与数据库绑定?
请个高手教一下TreeView从数据库中读取数据的过程.我在搞一个类似于QQ的软件,我怎么把数据库中好友表的数据绑定到TreeView里面.并且右键能打开对好友资料的查询...
请个高手教一下TreeView从数据库中读取数据的过程.我在搞一个类似于QQ的软件,我怎么把数据库中好友表的数据绑定到TreeView里面.并且右键能打开对好友资料的查询.请高手写下代码,最好有注释的.谢谢!或者加QQ:306057261 找位老师具体讲解下!
展开
4个回答
展开全部
/// <summary>
/// 动态绑定TreeView,递归
/// </summary>
/// <param name="tv">要绑定到的TreeView控件名称</param>
/// <param name="dt">准备绑定到TreeView控件的数据DataTable的名称</param>
/// <param name="Filterid">分级条件id,顶级分类ID为0</param>
/// <param name="node">上级TreeNode节点名称,没有上级节点或或全新绑定这里写(TreeNode)null</param>
/// <param name="Node_FLZD">节点的分级条件在数据库中的字段</param>
/// <param name="NodeText_ZD">节点绑定的显示的文本在数据库中的字段</param>
/// <param name="NodeTag_ZD">节点绑定的Tag值在数据库中的字段</param>
public static void bind_treeview(TreeView tv, DataTable dt, int Filterid, TreeNode node,string Node_FLZD,string NodeText_ZD,string NodeTag_ZD)//绑定treeView的方法。
{
DataView subView = dt.DefaultView;
subView.RowFilter = Node_FLZD + "=" + Filterid; //获得ID级别为当前ID的
foreach (DataRowView item in subView)
{
//如果上级ID为0就直接加载树下面
if (Filterid == 0)
{
TreeNode treenodel = new TreeNode(); //创建一个节点
treenodel.Text = item[NodeText_ZD].ToString(); //节点名称
int id = Convert.ToInt32(item[NodeTag_ZD]);
treenodel.Tag = id.ToString(); //节点值
tv.Nodes.Add(treenodel); // 如果级别ID为0就直接加到根节点
bind_treeview(tv, dt, id, treenodel, Node_FLZD, NodeText_ZD, NodeTag_ZD); // 递归调用
}
else
{
//不为0就加载传进来的子节点下
TreeNode treenodel = new TreeNode();
treenodel.Text = item[NodeText_ZD].ToString();
int id = Convert.ToInt32(item[NodeTag_ZD]);
treenodel.Tag = id.ToString();
treenodel.ExpandAll();
node.Nodes.Add(treenodel); //把节点加入到当前节点下,
bind_treeview(tv, dt, id, treenodel, Node_FLZD, NodeText_ZD, NodeTag_ZD);
}
}
}
/// 动态绑定TreeView,递归
/// </summary>
/// <param name="tv">要绑定到的TreeView控件名称</param>
/// <param name="dt">准备绑定到TreeView控件的数据DataTable的名称</param>
/// <param name="Filterid">分级条件id,顶级分类ID为0</param>
/// <param name="node">上级TreeNode节点名称,没有上级节点或或全新绑定这里写(TreeNode)null</param>
/// <param name="Node_FLZD">节点的分级条件在数据库中的字段</param>
/// <param name="NodeText_ZD">节点绑定的显示的文本在数据库中的字段</param>
/// <param name="NodeTag_ZD">节点绑定的Tag值在数据库中的字段</param>
public static void bind_treeview(TreeView tv, DataTable dt, int Filterid, TreeNode node,string Node_FLZD,string NodeText_ZD,string NodeTag_ZD)//绑定treeView的方法。
{
DataView subView = dt.DefaultView;
subView.RowFilter = Node_FLZD + "=" + Filterid; //获得ID级别为当前ID的
foreach (DataRowView item in subView)
{
//如果上级ID为0就直接加载树下面
if (Filterid == 0)
{
TreeNode treenodel = new TreeNode(); //创建一个节点
treenodel.Text = item[NodeText_ZD].ToString(); //节点名称
int id = Convert.ToInt32(item[NodeTag_ZD]);
treenodel.Tag = id.ToString(); //节点值
tv.Nodes.Add(treenodel); // 如果级别ID为0就直接加到根节点
bind_treeview(tv, dt, id, treenodel, Node_FLZD, NodeText_ZD, NodeTag_ZD); // 递归调用
}
else
{
//不为0就加载传进来的子节点下
TreeNode treenodel = new TreeNode();
treenodel.Text = item[NodeText_ZD].ToString();
int id = Convert.ToInt32(item[NodeTag_ZD]);
treenodel.Tag = id.ToString();
treenodel.ExpandAll();
node.Nodes.Add(treenodel); //把节点加入到当前节点下,
bind_treeview(tv, dt, id, treenodel, Node_FLZD, NodeText_ZD, NodeTag_ZD);
}
}
}
展开全部
递归方法。
// <summary>
/// TreeView数据绑定
/// </summary>
/// <param name="list"></param>
protected void CreateTreeView()
{
TreeNode rootNode = new TreeNode();
rootNode.Text = "全部";
rootNode.Value = "-1";
BasicTypeTreeView.Nodes.Add(rootNode);
BasicInformationManager BasicManager = new BasicInformationManager();
var tlist = BasicManager.GetAllBasicInfoTypes();
foreach (Model.BasicInforType mtype in tlist)
{
if (mtype.ParentID == 0)
{
TreeNode Node = new TreeNode();
//添加根节点
Node.Text = mtype.BasicTypeName;
Node.Value = mtype.ID.ToString();
Node.ToolTip = "编码" + mtype.BasicCode;
getSubNode(ref Node, mtype.ID);
rootNode.ChildNodes.Add(Node);
}
}
}
/// <summary>
/// 获取treeView子节点
/// </summary>
/// <param name="node"></param>
/// <param name="item"></param>
public void getSubNode(ref TreeNode node, int item)
{
BasicInformationManager BasicManager = new BasicInformationManager();
var tlist = BasicManager.GetBasicInforTypeByParentId(item);
if (tlist.Count > 0)
{
foreach (Model.BasicInforType m in tlist)
{
TreeNode subNode = new TreeNode();
subNode.Text = m.BasicTypeName;
subNode.Value = m.ID.ToString();
subNode.ToolTip = "编码" + m.BasicCode;
getSubNode(ref subNode, m.ID);
node.ChildNodes.Add(subNode); //进行子节点循环添加
}
}
}
// <summary>
/// TreeView数据绑定
/// </summary>
/// <param name="list"></param>
protected void CreateTreeView()
{
TreeNode rootNode = new TreeNode();
rootNode.Text = "全部";
rootNode.Value = "-1";
BasicTypeTreeView.Nodes.Add(rootNode);
BasicInformationManager BasicManager = new BasicInformationManager();
var tlist = BasicManager.GetAllBasicInfoTypes();
foreach (Model.BasicInforType mtype in tlist)
{
if (mtype.ParentID == 0)
{
TreeNode Node = new TreeNode();
//添加根节点
Node.Text = mtype.BasicTypeName;
Node.Value = mtype.ID.ToString();
Node.ToolTip = "编码" + mtype.BasicCode;
getSubNode(ref Node, mtype.ID);
rootNode.ChildNodes.Add(Node);
}
}
}
/// <summary>
/// 获取treeView子节点
/// </summary>
/// <param name="node"></param>
/// <param name="item"></param>
public void getSubNode(ref TreeNode node, int item)
{
BasicInformationManager BasicManager = new BasicInformationManager();
var tlist = BasicManager.GetBasicInforTypeByParentId(item);
if (tlist.Count > 0)
{
foreach (Model.BasicInforType m in tlist)
{
TreeNode subNode = new TreeNode();
subNode.Text = m.BasicTypeName;
subNode.Value = m.ID.ToString();
subNode.ToolTip = "编码" + m.BasicCode;
getSubNode(ref subNode, m.ID);
node.ChildNodes.Add(subNode); //进行子节点循环添加
}
}
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
先给你推荐一本C#的书:C#入门经典
绑定到TREEVIEW不用写代码,可以直接关连数据源。
右击用以下代码:
private void treeView_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
{
Point p1=new Point(e.X,e.Y);
TreeNode nod1=this.treeView.GetNodeAt(p1);
if (nod1!=null&&e.Button==MouseButtons.Right)
{
this.treeView.SelectedNode=nod1;
MessageBox.Show(this.treeView.SelectedNode.Text);
}
}
绑定到TREEVIEW不用写代码,可以直接关连数据源。
右击用以下代码:
private void treeView_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
{
Point p1=new Point(e.X,e.Y);
TreeNode nod1=this.treeView.GetNodeAt(p1);
if (nod1!=null&&e.Button==MouseButtons.Right)
{
this.treeView.SelectedNode=nod1;
MessageBox.Show(this.treeView.SelectedNode.Text);
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询