0

I have a tree of a depth of 2 as follows.

Module 1
    Form 1
         Tab 1
    Form 2
         Tab 2
         Tab 3
    Form 2
         Tab 2
         Tab 3
Module 2
    Form 4
         Tab 2
    Form 5
         Tab 4
         Tab 5
         Tab 6
    Form 5
         Tab 4
         Tab 5
         Tab 6
    Form 5
         Tab 4
         Tab 5
         Tab 6

As shown above, the problem is that, the Forms will be displayed as many times as the Tabs they have. For example, if Form 5 has three Tabs, it will be displayed thrice. I need them to be displayed just once since their tabs are the same.

This is my code:

private void LoadTreeview()
    {
        string sql = "SELECT Module, Form, Tab, HelpText " +
                        "FROM PageHelp " +
                        "WHERE Module IS NOT NULL";
        DataTable dt = Public_Methods.Get_DataTable(sql);

        DataView dvModules = new DataView(dt, "Module IS NOT NULL", "Module ASC", DataViewRowState.Unchanged);
        DataTable dtModules = dvModules.ToTable(true, "Module");

        for (int count = 0; count < dtModules.Rows.Count; count++)
        {
            TreeNode TModule = new TreeNode();
            TModule.Value = dtModules.Rows[count]["Module"].ToString();
            TModule.Text = dtModules.Rows[count]["Module"].ToString();
            TModule.Collapse();

            LoadForms(ref TModule, dt);
            TreeView_EditHelp_Help.Nodes.Add(TModule);                
        }
    }

    private void LoadForms(ref TreeNode TModule, DataTable dt)
    {
        DataRow[] drForms = dt.Select("Module='" + TModule.Value + "'");

        for (int count = 0; count < drForms.Length; count++)
        {
            TreeNode TForm = new TreeNode();
            TForm.Value = drForms[count]["Form"].ToString();
            TForm.Text = drForms[count]["Form"].ToString();
            TForm.Collapse();

            LoadTabs(ref TForm, dt);
            TModule.ChildNodes.Add(TForm);
        }
    }

    private void LoadTabs(ref TreeNode TForm, DataTable dt)
    {
        DataRow[] drTabs = dt.Select("Form='" + TForm.Value + "'");
        for (int count = 0; count < drTabs.Length; count++)
        {
            TreeNode TTab = new TreeNode();
            TTab.Value = drTabs[count]["Tab"].ToString();
            TTab.Text = drTabs[count]["Tab"].ToString();
            TTab.Collapse();
            TForm.ChildNodes.Add(TTab);
        }
    }

Any assistance please?

4

2 回答 2

2
DataRow[] drForms = dt.Select("Module='" + TModule.Value + "'");

改成:

DataRow[] drForms = dt.Select("Module='" + TModule.Value + "'").CopyToDataTable().DefaultView.ToTable(true,"Form").Select();
于 2013-07-31T10:54:34.920 回答
0

试试下面的一些东西

for (int count = 0; count < dtModules.Rows.Count; count++)
        {
            TreeNode TModule = new TreeNode();
            TModule.Value = dtModules.Rows[count]["Module"].ToString();
            TModule.Text = dtModules.Rows[count]["Module"].ToString();
            TModule.Collapse();
f =0;
             for(int j=0;j<count;j++)
{  // check the new module has been previously ocured or not
 if(dtModules.Rows[count]["Module"].ToString() == dtModules.Rows[j]["Module"].ToString())
  {  
 f=1;
break;
}
}

 if(f== 0){
            LoadForms(ref TModule, dt);
            TreeView_EditHelp_Help.Nodes.Add(TModule);                
}
        }

您也可以创建单独的功能进行检查

于 2013-07-31T11:04:28.497 回答