Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/294.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/82.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# TreeView多个父母_C#_Sql_Winforms_Treeview - Fatal编程技术网

C# TreeView多个父母

C# TreeView多个父母,c#,sql,winforms,treeview,C#,Sql,Winforms,Treeview,我目前有一个treeview,它从一个SQL数据库表加载数据。我已经让它加载了结果,但是我发现它为每个子对象加载了相同的父节点,并且下面只列出了一个子对象。我如何将它们分组,使一个父项与多个子项相关联?下面是我的代码。我尝试使用DISTINCT,但我不确定如何使用DISTINCT,当我必须拉取多个列时,无法使用DISTINCT。我非常感谢任何人的帮助 例如,我的表格有: WorkOrderName ItemNumber 45123 101 45123

我目前有一个treeview,它从一个SQL数据库表加载数据。我已经让它加载了结果,但是我发现它为每个子对象加载了相同的父节点,并且下面只列出了一个子对象。我如何将它们分组,使一个父项与多个子项相关联?下面是我的代码。我尝试使用DISTINCT,但我不确定如何使用DISTINCT,当我必须拉取多个列时,无法使用DISTINCT。我非常感谢任何人的帮助

例如,我的表格有:

WorkOrderName       ItemNumber
45123               101
45123               102
45123               103
我的treeview当前看起来像:

+ 45123
  - 101
+ 45123
  - 102
+ 45123
  - 103
当我需要它看起来像这样时:

+ 45123
  - 101
  - 102
  - 103
以下是我的代码:

        private void Form1_Shown(object sender, EventArgs e)
        {
        treeView1.Nodes.Clear();

        try
        {
            cn.Open();
        }
        catch(SqlException ex)
        {
            MessageBox.Show(ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
            Application.ExitThread();
        }

        SqlCommand cm = new SqlCommand("SELECT * FROM ProductTracking WHERE WorkOrderName IS NOT NULL ORDER BY WorkOrderName ASC", cn);

        try
        {
            SqlDataReader dr = cm.ExecuteReader();

            while (dr.Read())
            {
                TreeNode node = new TreeNode(dr["WorkOrderName"].ToString());
                node.Nodes.Add(dr["ItemNumber"].ToString());

                treeView1.Nodes.Add(node);
            }
        }
        catch(Exception ex)
        {
            MessageBox.Show(ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
    }

好的,作为一个简单的修复,您可以先将数据转储到字典中:

免责声明:这只是一个快速修复;完全写在我的头上。i、 e:可以编写更高效的代码

try
{
    SqlDataReader dr = cm.ExecuteReader();

    //create a dict of strings which holds a list of "items"
    var dict = new Dictionary<string, List<string>>();

    while (dr.Read())
    {
        var orderName = (dr["WorkOrderName"].ToString();
        //fill the dictionary
        if (!dict.ContainsKey(orderName))
            dict.Add(orderName, new List<string>());

        dict[orderName].Add(dr["ItemNumber"].ToString());
    }

    //this should also be possible with a single linq statement
    //now loop the dictionary and fill the tree
    foreach (var key in dict.Keys)
    {
       //add parent
       TreeNode node = new TreeNode(key);

       //add childs
       foreach(var item in dict[key])
       {
           node.Nodes.Add(item);
       }

       //add it to the treeview
       treeView1.Nodes.Add(node);
    }
}

由于您已经拥有WorkOrderName顺序中的数据,因此只需跟踪父节点:

TreeNode parentNode = null;
while (dr.Read()) {
  string parentKey = dr["WorkOrderName"].ToString();
  if (parentNode == null || parentNode.Name != parentKey) {
    parentNode = treeView1.Nodes.Add(parentKey, parentKey);
  }
  parentNode.Nodes.Add(dr["ItemNumber"].ToString());
}

那么,您将为每个数据库结果创建一个父节点。所以您应该检查父节点是否已经存在,如果存在则检查父节点,如果不存在则检查不存在。不幸的是,我不知道如何在代码中实现这一点。在此之前我没有使用过TreeView。