C#中treeview与数据库的连接
在web里,将已经有的数据库按照姓名和级数,排序到treeview中,同时treeview点击子节点,可以跳转到每个人的详细界面。如果不用treeview,还有什么控件可...
在web里,将已经有的数据库按照姓名和级数,排序到treeview中,同时treeview点击子节点,可以跳转到每个人的详细界面。如果不用treeview,还有什么控件可以和数据库相连。请高手回答一下。在线求助。留QQ帮助也可以哈~
上图为数据库中数据
上图为想用的treeview形式,将这些新建节点的名称和数据库连接,将节点名称变成数据库里的姓名。形成一个动态的效果~不是静态~ 展开
上图为数据库中数据
上图为想用的treeview形式,将这些新建节点的名称和数据库连接,将节点名称变成数据库里的姓名。形成一个动态的效果~不是静态~ 展开
展开全部
我简单写下代码,我的思想是使用数据库链表填充,先看代码吧:
//数据库怎么操作读取数据我就不写了,假如我现在有了dataset对象ds,在查数据表的时候我们就把顺序排列好
for(int i=0;i<ds.Tables[0].Rows.Count;i++)
{
tree.Nodes.Add(ds.Tables[0].Rows[i]["列名"].ToString());
}
如果要加入子节点,那我们就加完父节点再查找子节点,挨个加进去:
//建立个ds返回对象,以父节点作为参数
for(int i=0;i<ds.Tables[0].Rows.Count;i++)
{
tree.Nodes.Add(ds.Tables[0].Rows[i]["列名"].ToString());
}
foreach(TreeNodes td in tree.Nodes)
{
for(int j=0;j<td.Text).Tables[0].Rows.Count;i++)
{
td.Add(dss(td.Text).Tables[0].Rows[j]["列名"].ToString());
}
}
展开全部
SqlConnection conn = new SqlConnection(server=.;uid=sa;pwd=数据库登录密码,database=要连接的数据库名称);
/// <summary>
/// 此处是一个方法
/// </summary>
/// <param name="sqlstr">传入一个SQL执行语句</param>
/// <param name="parm">传入存储过程里面所需要的参数</param>
/// <returns>返回内存集中的</returns>
public static DataTable GetDataSet(string sqlstr, params object[] parm)
{
//创建数据适配器
SqlDataAdapter sda = new SqlDataAdapter(sqlstr, conn);
//判断参数个数
if (parm.Length > 0)
{
//循环参数个数
for (int i = 0; i < parm.Length; i++)
{
sda.SelectCommand.Parameters.AddWithValue("@" + i, parm[i]);
}
}
//创建内存集
DataTable ds = new DataTable();
//填充内存集
sda.Fill(ds);
return ds;
}
//清除节点
treeView1.Nodes.Clear();
DataTable one =GetDataSet("select 姓名 from 要查询的表名 where 级数=1");
foreach (DataRow x in one.Rows)
{
//创建节点
TreeNode tn = new TreeNode(x["姓名"].ToString());
tn.Name = x["级数"].ToString();
//将这个节点添加到根节点
treeView1.Nodes.Add(tn);
DataTable two= GetDataSet("select 姓名 from 要查询的表名 where 级数=2");
foreach (DataRow y in two.Rows)
{
//创建节点
TreeNode tn2 = new TreeNode(y["姓名"].ToString());
tn2.Name = y["级数"].ToString();
//为上一级节点添加他的子节点
tn.Nodes.Add(tn2);
DataTable dtemp = GetDataSet("select 姓名 from 要查询的表名 where 级数=3)
foreach (DataRow z in dtemp.Rows)
{
//创建节点
TreeNode tn3 = new TreeNode(z["姓名"].ToString());
tn3.Name = z["级数"].ToString();
//为上一级节点添加他的子节点
tn2.Nodes.Add(tn3);
}
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
方法1:
DB DBManager = new DB();//实例化数据库操作类
string strConn = System.Configuration.ConfigurationManager.ConnectionStrings["rizhi"].ConnectionString;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
//TreeView1.Attributes.Add("onclick", "OnTreeNodeChecked()");
TreeView1.Attributes.Add("onclick", "testClick(event);return true;");//单击文字,CheckBox=true
TreeView1.ShowCheckBoxes = TreeNodeTypes.Leaf;//最后一级选中
TreeView1.ExpandDepth = 0;//不展开
using (SqlConnection conn = new SqlConnection(strConn))
{
DataSet ds_company = new DataSet();//实例化数据集(公司)
SqlDataAdapter sda_company = new SqlDataAdapter("select * from [company] where id<>"+0+" and id<>"+1001, conn);//实例化数据适配器(公司)
sda_company.Fill(ds_company, "company");//虚拟表填充到内存
for (int i = 0; i < ds_company.Tables["company"].Rows.Count; i++)//循环所有"公司"
{
TreeNode td_company = new TreeNode();//实例化公司节点
td_company.Text = ds_company.Tables["company"].Rows["company"].ToString();//公司节点名称
TreeView1.Nodes.Add(td_company);//将公司节点添加至"树"
td_company.SelectAction = TreeNodeSelectAction.Expand;
DataSet ds_department = new DataSet();//实例化数据集(部门)
SqlDataAdapter sda_department = new SqlDataAdapter("select * from [department] where [coid]=" + ds_company.Tables["company"].Rows["id"]+" and department<>'全公司'" , conn);//实例化数据库适配器(部门)
sda_department.Fill(ds_department, "department");//虚拟表填充到内存
for (int j = 0; j < ds_department.Tables["department"].Rows.Count; j++)//循环 ds.Tables["department"].Rows["id"] 对应的所有部门
{
TreeNode td_department = new TreeNode();//实例化部门节点
td_department.Text = ds_department.Tables["department"].Rows[j]["department"].ToString();//部门节点名称
td_company.ChildNodes.Add(td_department);//填充部门节点至"树"
td_department.SelectAction = TreeNodeSelectAction.Expand;//只允许展开
#region 如果人员属于某个班组
if (DBManager.getDs("select [info] from [user] where [pid]=" + ds_department.Tables["department"].Rows[j]["id"] + "", "is").Tables["is"].Rows.Count == 0)
{
DataSet ds_class = new DataSet();//实例化数据集(班组)
SqlDataAdapter sda_class = new SqlDataAdapter("select * from [class] where [deid]=" + ds_department.Tables["department"].Rows[j]["id"], conn);//实例化数据库适配器(班组)
sda_class.Fill(ds_class, "class");//虚拟表填充到内存
for (int z = 0; z < ds_class.Tables["class"].Rows.Count; z++)//循环 ds_department.Tables["class"].Rows[j]["id"] 对应的所有班组
{
TreeNode td_class = new TreeNode();//实例化班组节点
td_class.Text = ds_class.Tables["class"].Rows[z]["class"].ToString();//班组节点名称
td_department.ChildNodes.Add(td_class);//填充班组节点至"树"
td_class.SelectAction = TreeNodeSelectAction.Expand;//只允许展开
DataSet ds_user = new DataSet();//实例化数据集(人员)
SqlDataAdapter sda_user = new SqlDataAdapter("select [name] from [user] where [pid]=" + ds_class.Tables["class"].Rows[z]["id"] + "", conn);////实例化数据库适配器(人员)
sda_user.Fill(ds_user, "user");//虚拟表填充到内存
for (int k = 0; k < ds_user.Tables["user"].Rows.Count; k++)//循环 ds_department.Tables["user"].Rows[k]["name"] 对应的所有人员
{
TreeNode td_user = new TreeNode();//实例化班组节点
td_user.Text = ds_user.Tables["user"].Rows[k]["name"].ToString();//人员节点名称
td_class.ChildNodes.Add(td_user);//填充人员节点至"树"
td_user.NavigateUrl = "javascript:void(0)";//选中,但无效
}
}
}
#endregion
#region 如果人员直接属于某个部门
else
{
DataSet ds_user = new DataSet();
SqlDataAdapter sda_user = new SqlDataAdapter("select [name] from [user] where [pid]=" + ds_department.Tables["department"].Rows[j]["id"] + "", conn);
sda_user.Fill(ds_user, "user");
for (int k = 0; k < ds_user.Tables["user"].Rows.Count; k++)
{
TreeNode td_user = new TreeNode();
td_user.Text = ds_user.Tables["user"].Rows[k]["name"].ToString();
td_department.ChildNodes.Add(td_user);
td_user.NavigateUrl = "javascript:void(0)";
}
}
#endregion
}
}
}
}
}
方法2:
string strConn = System.Configuration.ConfigurationManager.ConnectionStrings["rizhi"].ConnectionString;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DataSet ds = new DataSet();
SqlConnection conn = new SqlConnection(strConn);
using (conn)
{
SqlDataAdapter sda_Company = new SqlDataAdapter("select [id],[company] from [company]", conn);
sda_Company.Fill(ds, "company");
TreeNode tn_Company;
TreeNode tn_Department;
TreeNode tn_Class;
foreach (DataRow company_row in ds.Tables["company"].Rows)
{
tn_Company = new TreeNode();
tn_Company.Text = company_row["company"].ToString();
tn_Company.Value = company_row["id"].ToString();
TreeView1.Nodes.Add(tn_Company);
SqlDataAdapter sda_Department = new SqlDataAdapter("select [id],[department] from [department] where coid=" + tn_Company.Value, conn);
sda_Department.Fill(ds, "department");
if (ds.Tables["department"].Rows.Count > 0)
{
foreach (DataRow department_row in ds.Tables["department"].Rows)
{
tn_Department = new TreeNode();
tn_Department.Text = department_row["department"].ToString();
tn_Department.Value = department_row["id"].ToString();
tn_Company.ChildNodes.Add(tn_Department);
SqlDataAdapter sda_Class = new SqlDataAdapter("select [id],[class] from [class] where deid=" + tn_Department.Value, conn);
sda_Class.Fill(ds, "class");
if (ds.Tables["class"].Rows.Count > 0)
{
foreach (DataRow class_row in ds.Tables["class"].Rows)
{
tn_Class = new TreeNode();
tn_Class.Text = class_row["class"].ToString();
tn_Class.Value = class_row["id"].ToString();
tn_Department.ChildNodes.Add(tn_Class);
}
ds.Tables["class"].Clear();
}
sda_Class.Dispose();
}
ds.Tables["department"].Clear();
}
sda_Department.Dispose();
}
sda_Company.Dispose();
}
}
}
方法3:
public void bindTv()
{
ds = new DataSet();
using (SqlConnection conn = new SqlConnection(strConn))
{
conn.Open();
SqlDataAdapter sda = new SqlDataAdapter("select * from [class]", conn);
sda.Fill(ds);
}
AddTree(101, (TreeNode)null);
}
public void AddTree(int ParentID,TreeNode pNode)
{
DataView dvTree = new DataView(ds.Tables[0]);
string Fstr = "[deid]=" + ParentID;
dvTree.RowFilter = Fstr;
foreach (DataRowView Row in dvTree)
{
TreeNode Node = new TreeNode();
if (pNode == null)
{
Node.Value = Row["id"].ToString();
Node.Text = Row["class"].ToString();
TreeView1.Nodes.Add(Node);
AddTree(Int32.Parse(Row["id"].ToString()), Node);
}
else
{
Node.Value = Row["id"].ToString();
Node.Text = Row["class"].ToString();
pNode.ChildNodes.Add(Node);
AddTree(Int32.Parse(Row["id"].ToString()), Node);
}
}
}
以上是treeview的三种数据库绑定方法,你可以对照的看下
修改为你自己的就行
希望我的回答对您有帮助,若有帮助的话请及时采纳哈
DB DBManager = new DB();//实例化数据库操作类
string strConn = System.Configuration.ConfigurationManager.ConnectionStrings["rizhi"].ConnectionString;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
//TreeView1.Attributes.Add("onclick", "OnTreeNodeChecked()");
TreeView1.Attributes.Add("onclick", "testClick(event);return true;");//单击文字,CheckBox=true
TreeView1.ShowCheckBoxes = TreeNodeTypes.Leaf;//最后一级选中
TreeView1.ExpandDepth = 0;//不展开
using (SqlConnection conn = new SqlConnection(strConn))
{
DataSet ds_company = new DataSet();//实例化数据集(公司)
SqlDataAdapter sda_company = new SqlDataAdapter("select * from [company] where id<>"+0+" and id<>"+1001, conn);//实例化数据适配器(公司)
sda_company.Fill(ds_company, "company");//虚拟表填充到内存
for (int i = 0; i < ds_company.Tables["company"].Rows.Count; i++)//循环所有"公司"
{
TreeNode td_company = new TreeNode();//实例化公司节点
td_company.Text = ds_company.Tables["company"].Rows["company"].ToString();//公司节点名称
TreeView1.Nodes.Add(td_company);//将公司节点添加至"树"
td_company.SelectAction = TreeNodeSelectAction.Expand;
DataSet ds_department = new DataSet();//实例化数据集(部门)
SqlDataAdapter sda_department = new SqlDataAdapter("select * from [department] where [coid]=" + ds_company.Tables["company"].Rows["id"]+" and department<>'全公司'" , conn);//实例化数据库适配器(部门)
sda_department.Fill(ds_department, "department");//虚拟表填充到内存
for (int j = 0; j < ds_department.Tables["department"].Rows.Count; j++)//循环 ds.Tables["department"].Rows["id"] 对应的所有部门
{
TreeNode td_department = new TreeNode();//实例化部门节点
td_department.Text = ds_department.Tables["department"].Rows[j]["department"].ToString();//部门节点名称
td_company.ChildNodes.Add(td_department);//填充部门节点至"树"
td_department.SelectAction = TreeNodeSelectAction.Expand;//只允许展开
#region 如果人员属于某个班组
if (DBManager.getDs("select [info] from [user] where [pid]=" + ds_department.Tables["department"].Rows[j]["id"] + "", "is").Tables["is"].Rows.Count == 0)
{
DataSet ds_class = new DataSet();//实例化数据集(班组)
SqlDataAdapter sda_class = new SqlDataAdapter("select * from [class] where [deid]=" + ds_department.Tables["department"].Rows[j]["id"], conn);//实例化数据库适配器(班组)
sda_class.Fill(ds_class, "class");//虚拟表填充到内存
for (int z = 0; z < ds_class.Tables["class"].Rows.Count; z++)//循环 ds_department.Tables["class"].Rows[j]["id"] 对应的所有班组
{
TreeNode td_class = new TreeNode();//实例化班组节点
td_class.Text = ds_class.Tables["class"].Rows[z]["class"].ToString();//班组节点名称
td_department.ChildNodes.Add(td_class);//填充班组节点至"树"
td_class.SelectAction = TreeNodeSelectAction.Expand;//只允许展开
DataSet ds_user = new DataSet();//实例化数据集(人员)
SqlDataAdapter sda_user = new SqlDataAdapter("select [name] from [user] where [pid]=" + ds_class.Tables["class"].Rows[z]["id"] + "", conn);////实例化数据库适配器(人员)
sda_user.Fill(ds_user, "user");//虚拟表填充到内存
for (int k = 0; k < ds_user.Tables["user"].Rows.Count; k++)//循环 ds_department.Tables["user"].Rows[k]["name"] 对应的所有人员
{
TreeNode td_user = new TreeNode();//实例化班组节点
td_user.Text = ds_user.Tables["user"].Rows[k]["name"].ToString();//人员节点名称
td_class.ChildNodes.Add(td_user);//填充人员节点至"树"
td_user.NavigateUrl = "javascript:void(0)";//选中,但无效
}
}
}
#endregion
#region 如果人员直接属于某个部门
else
{
DataSet ds_user = new DataSet();
SqlDataAdapter sda_user = new SqlDataAdapter("select [name] from [user] where [pid]=" + ds_department.Tables["department"].Rows[j]["id"] + "", conn);
sda_user.Fill(ds_user, "user");
for (int k = 0; k < ds_user.Tables["user"].Rows.Count; k++)
{
TreeNode td_user = new TreeNode();
td_user.Text = ds_user.Tables["user"].Rows[k]["name"].ToString();
td_department.ChildNodes.Add(td_user);
td_user.NavigateUrl = "javascript:void(0)";
}
}
#endregion
}
}
}
}
}
方法2:
string strConn = System.Configuration.ConfigurationManager.ConnectionStrings["rizhi"].ConnectionString;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DataSet ds = new DataSet();
SqlConnection conn = new SqlConnection(strConn);
using (conn)
{
SqlDataAdapter sda_Company = new SqlDataAdapter("select [id],[company] from [company]", conn);
sda_Company.Fill(ds, "company");
TreeNode tn_Company;
TreeNode tn_Department;
TreeNode tn_Class;
foreach (DataRow company_row in ds.Tables["company"].Rows)
{
tn_Company = new TreeNode();
tn_Company.Text = company_row["company"].ToString();
tn_Company.Value = company_row["id"].ToString();
TreeView1.Nodes.Add(tn_Company);
SqlDataAdapter sda_Department = new SqlDataAdapter("select [id],[department] from [department] where coid=" + tn_Company.Value, conn);
sda_Department.Fill(ds, "department");
if (ds.Tables["department"].Rows.Count > 0)
{
foreach (DataRow department_row in ds.Tables["department"].Rows)
{
tn_Department = new TreeNode();
tn_Department.Text = department_row["department"].ToString();
tn_Department.Value = department_row["id"].ToString();
tn_Company.ChildNodes.Add(tn_Department);
SqlDataAdapter sda_Class = new SqlDataAdapter("select [id],[class] from [class] where deid=" + tn_Department.Value, conn);
sda_Class.Fill(ds, "class");
if (ds.Tables["class"].Rows.Count > 0)
{
foreach (DataRow class_row in ds.Tables["class"].Rows)
{
tn_Class = new TreeNode();
tn_Class.Text = class_row["class"].ToString();
tn_Class.Value = class_row["id"].ToString();
tn_Department.ChildNodes.Add(tn_Class);
}
ds.Tables["class"].Clear();
}
sda_Class.Dispose();
}
ds.Tables["department"].Clear();
}
sda_Department.Dispose();
}
sda_Company.Dispose();
}
}
}
方法3:
public void bindTv()
{
ds = new DataSet();
using (SqlConnection conn = new SqlConnection(strConn))
{
conn.Open();
SqlDataAdapter sda = new SqlDataAdapter("select * from [class]", conn);
sda.Fill(ds);
}
AddTree(101, (TreeNode)null);
}
public void AddTree(int ParentID,TreeNode pNode)
{
DataView dvTree = new DataView(ds.Tables[0]);
string Fstr = "[deid]=" + ParentID;
dvTree.RowFilter = Fstr;
foreach (DataRowView Row in dvTree)
{
TreeNode Node = new TreeNode();
if (pNode == null)
{
Node.Value = Row["id"].ToString();
Node.Text = Row["class"].ToString();
TreeView1.Nodes.Add(Node);
AddTree(Int32.Parse(Row["id"].ToString()), Node);
}
else
{
Node.Value = Row["id"].ToString();
Node.Text = Row["class"].ToString();
pNode.ChildNodes.Add(Node);
AddTree(Int32.Parse(Row["id"].ToString()), Node);
}
}
}
以上是treeview的三种数据库绑定方法,你可以对照的看下
修改为你自己的就行
希望我的回答对您有帮助,若有帮助的话请及时采纳哈
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
递归绑定!
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询