在C#WPF中将Treeview与数据库绑定

时间:2019-03-06 20:34:31

标签: c# wpf

我有一个数据库,该数据库需要与Treeview绑定。问题是它没有ParentId。而是分步进行。

enter image description here 我希望我的树状视图如图所示,enter image description here 我是WPF的新手,treeview是一个新概念。我无法从哪里开始。我了解了分层数据模板。但是我不知道如何使用该数据库。我找不到类似这样的适当解决方案。所以只有我在这里发布问题。

这是我到目前为止尝试过的:

OleDbConnection oledb = new OleDbConnection(db);
OleDbDataAdapter oleda = new OleDbDataAdapter();
OleDbCommand olecmd = new OleDbCommand("SELECT [dId],[step1],[step2],[step3],[step4],[step5],[dname] FROM [drinks] where [step1] <> 0 and[step2] = 0 and[step3] = 0 and[step4] = 0 and[step5] = 0", oledb);
oleda.SelectCommand = olecmd;
OleDbCommandBuilder olecb = new OleDbCommandBuilder(oleda);
DataSet ds = new DataSet();
oleda.Fill(ds);

TreeViewItem topnode = new TreeViewItem();
topnode.Header = "Drinks";
treeview1.Items.Add(topnode);
foreach (DataRow dr in ds.Tables[0].Rows)
{
    //System.Windows.MessageBox.Show(dr["dname"].ToString());
    TreeViewItem Step1node = new TreeViewItem();
    Step1node.Header = dr["dname"].ToString();
    topnode.Items.Add(Step1node);
    var step1 = "";
    OleDbCommand olecmd1 = new OleDbCommand("SELECT [dId],[step1],[step2],[step3],[step4],[step5],[dname] FROM [drinks] where [step1] = ? and[step2] <> 0 and[step3] = 0 and[step4] = 0 and[step5] = 0", oledb);
    olecmd1.Parameters.AddWithValue(@step1, dr["step1"].ToString());
    OleDbDataAdapter oleda1 = new OleDbDataAdapter(olecmd1);
    DataSet ds1 = new DataSet();
    oleda1.Fill(ds1);
    foreach (DataRow dr1 in ds1.Tables[0].Rows)
    {
       //System.Windows.MessageBox.Show(dr1["dname"].ToString());
        TreeViewItem Step2node = new TreeViewItem();
        Step2node.Header = dr1["dname"].ToString();
        Step1node.Items.Add(Step2node);
        var step2 = "";
        OleDbCommand olecmd2 = new OleDbCommand("SELECT [dId],[step1],[step2],[step3],[step4],[step5],[dname] FROM [drinks] where [step1] = ? and[step2] = ? and[step3] <> 0 and[step4] = 0 and[step5] = 0", oledb);
        olecmd2.Parameters.AddWithValue(@step1, dr["step1"].ToString());
        olecmd2.Parameters.AddWithValue(@step2, dr1["step2"].ToString());
        OleDbDataAdapter oleda2 = new OleDbDataAdapter(olecmd2);
        DataSet ds2 = new DataSet();
        oleda2.Fill(ds2);
        foreach (DataRow dr2 in ds2.Tables[0].Rows)
        {
            TreeViewItem Step3node = new TreeViewItem();
            Step3node.Header = dr2["dname"].ToString();
            Step2node.Items.Add(Step3node);
            var step3 = "";
            OleDbCommand olecmd3 = new OleDbCommand("SELECT [dId],[step1],[step2],[step3],[step4],[step5],[dname] FROM [drinks] where [step1] = ? and[step2] = ? and[step3] = ? and[step4] <> 0 and[step5] = 0", oledb);
            olecmd3.Parameters.AddWithValue(@step1, dr["step1"].ToString());
            olecmd3.Parameters.AddWithValue(@step2, dr1["step2"].ToString());
            olecmd3.Parameters.AddWithValue(@step3, dr2["step3"].ToString());
            OleDbDataAdapter oleda3 = new OleDbDataAdapter(olecmd3);
            DataSet ds3 = new DataSet();
            oleda3.Fill(ds3);
            foreach (DataRow dr3 in ds3.Tables[0].Rows)
            {
                TreeViewItem Step4node = new TreeViewItem();
                Step4node.Header = dr3["dname"].ToString();
                Step3node.Items.Add(Step4node);
                var step4 = "";
                OleDbCommand olecmd4 = new OleDbCommand("SELECT [dId],[step1],[step2],[step3],[step4],[step5],[dname] FROM [drinks] where [step1] = ? and[step2] = ? and[step3] = ? and[step4]  = ? and[step5] <> 0", oledb);

                olecmd4.Parameters.AddWithValue(@step1, dr["step1"].ToString());
                olecmd4.Parameters.AddWithValue(@step2, dr1["step2"].ToString());
                olecmd4.Parameters.AddWithValue(@step3, dr2["step3"].ToString());
                olecmd4.Parameters.AddWithValue(@step4, dr3["step4"].ToString());
                OleDbDataAdapter oleda4 = new OleDbDataAdapter(olecmd4);
                DataSet ds4 = new DataSet();
                oleda4.Fill(ds4);
                foreach (DataRow dr4 in ds4.Tables[0].Rows)
                {
                    TreeViewItem Step5node = new TreeViewItem();

                    Step5node.Header = dr4["dname"].ToString();
                    Step4node.Items.Add(Step5node);

                }
            }
        }
    }
}

0 个答案:

没有答案