我有一个包含两列的表,我希望使用Windows窗体(.NET 4.0)在Oracle树视图中使用Oracle数据库绑定分层显示它们
ORGID, PARENTID
======================
1 -
2 1
3 1
4 3
.....
ORGID是父级,PARENTID是子级(也可以是父级)。
我试过这个:
public partial class OrgHierarchy : Form
{
DataSet ds = new DataSet();
TreeNode parentNode = null;
DataSet RunQuery(String Query)
{
DataSet ds = new DataSet();
OracleConnection con = new OracleConnection(OrgScheme.GetConnectionString());
DataTable dt = new DataTable();
OracleCommand cmd = new OracleCommand();
cmd.CommandText = Query;
cmd.CommandType = CommandType.Text;
cmd.Connection = con;
con.Open();
OracleDataAdapter da = new OracleDataAdapter(cmd);
ds = new DataSet();
da.Fill(ds);
return ds;
}
void CreateNode(TreeNode node)
{
DataSet ds = RunQuery("select orgid ,parentid ... 0 and parentid=" + node.Value +" order by parentid,orgid");
if (ds.Tables[0].Rows.Count == 0)
{
return;
}
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
TreeNode tnode = new TreeNode(ds.Tables[0].Rows[i][1].ToString(), ds.Tables[0].Rows[i][0].ToString());
tnode.SelectAction = TreeNodeSelectAction.Expand;
node.ChildNodes.Add(tnode);
CreateNode(tnode);
}
}
public OrgHierarchy()
{
InitializeComponent();
DataSet ds = RunQuery("select orgid ,parentid .... and parentid is null");
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
TreeNode root = new TreeNode(ds.Tables[0].Rows[i][1].ToString(), ds.Tables[0].Rows[i][0].ToString());
root.SelectAction = TreeNodeSelectAction.Expand;
CreateNode(root);
OrgTree.Nodes.Add(root);
}
}
}
我有很多错误:
错误1&#39; System.Windows.Forms.TreeNode&#39;不包含&#39;价值&#39;的定义没有延伸方法&#39;价值&#39;接受类型为System.Windows.Forms.TreeNode&#39;的第一个参数。可以找到(你错过了......
错误2最佳重载方法匹配&#39; System.Windows.Forms.TreeNode.TreeNode(string,System.Windows.Forms.TreeNode [])&#39;有一些无效的论点
请帮忙吗?我想我使用的是Web控件(.Value),我无法使用WinForms。
答案 0 :(得分:1)
首先,我改变了你的代码的一些逻辑
DataSet ds = new DataSet();
TreeNode parentNode = null;
TreeView treeview = null;
public OrgHierarchy()
{
InitializeComponent();
DataSet ds = RunQuery("select orgid ,parentid .... and parentid is null");
DataTable dt = ds.Tables[0];
settingRootLevel(dt);
if (treeview.Nodes.Count > 0)
{
for (int i = 0; i < treeview.Nodes.Count; i++)
{
TreeNode node = treeview.Nodes[i];
addChildNodes(dt, node);
}
}
this.Controls.Add(treeview);
}
DataSet RunQuery(String Query)
{
DataSet ds = new DataSet();
OracleConnection con = new OracleConnection(OrgScheme.GetConnectionString());
DataTable dt = new DataTable();
OracleCommand cmd = new OracleCommand();
cmd.CommandText = Query;
cmd.CommandType = CommandType.Text;
cmd.Connection = con;
con.Open();
OracleDataAdapter da = new OracleDataAdapter(cmd);
ds = new DataSet();
da.Fill(ds);
return ds;
}
public void addChildNodes(DataTable dt,TreeNode Node)
{
DataRow[] datarows = dt.Select("PARENTID = " + Node.Text);
for (int i = 0; i < datarows.Length; i++)
{
TreeNode node = new TreeNode(datarows[i][0].ToString());
Node.Nodes.Add(node);
addChildNodes(dt, node);
}
}
public void settingRootLevel(DataTable dt)
{
DataRow[] datarows = dt.Select("PARENTID is null");
foreach (DataRow dr in datarows)
{
treeview.Nodes.Add(dr[0].ToString());
}
}
其中settingRootLevel
设置所有父节点,addChildNodes
运行所有带递归的子节点,还有一件事要清除,如果发生任何异常,请在try catch中创建此代码。