C#Treeview与存储过程

时间:2016-02-01 07:33:20

标签: c# treeview hierarchicaldatatemplate

我会从存储过程填充树视图。

该程序返回一系列季节和剧集。

program_id  program_header_id   title
1           Null                Game of Thrones
2           1                   Saison 1
3           1                   Saison 2
4           1                   Saison 3
5           2                   Episode 1
6           2                   Episode 2
7           2                   Episode 3
8           2                   Episode 4
9           3                   Episode 1
10          3                   Episode 2
11          4                   Episode 1

我想在树视图中看到这个

-   Game of Thrones
    -   Saison 1
        -   Episode 1
        -   Episode 2
        -   Episode 3
        -   Episode 4
    -   Saison 2
        -   Episode 1
        -   Episode 2
    -   Saison 3
        -   Episode 1

这是我填写树视图的代码

SqlCommand cmd;
cmd = new SqlCommand("S_fiction_program_search_v3", mycon);
cmd.CommandType = CommandType.StoredProcedure;
DataTable dt = new DataTable("treeProgram");
SqlDataAdapter dataAdp = new SqlDataAdapter(cmd);
dataAdp.Fill(dt);
DataSet ds = new DataSet();
ds.Tables.Add(dt);
ds.Relations.Add("rsParentChild", ds.Tables["treeProgram"].Columns["program_id"], ds.Tables["treeProgram"].Columns["program_header_id"]);
tvMain.ItemsSource = ds.Tables["treeProgram"].DefaultView;

和xaml:

<TreeView Name="tvMain" Grid.Row="2">
    <TreeView.ItemTemplate>
        <HierarchicalDataTemplate ItemsSource="{Binding rsParentChild}">
            <TextBlock Text="{Binding original_title}" />
        </HierarchicalDataTemplate>
    </TreeView.ItemTemplate>
</TreeView>

现在,树视图就像这样填充:

-   Game of Thrones
        -   Saison 1
            -   Episode 1
            -   Episode 2
            -   Episode 3
            -   Episode 4
        -   Saison 2
            -   Episode 1
            -   Episode 2
        -   Saison 3
            -   Episode 1
-   Saison 1
    -   Episode 1
    -   Episode 2
    -   Episode 3
    -   Episode 4
-   Saison 2
    -   Episode 1
    -   Episode 2
-   Saison 3
-   Episode 1
-   Episode 1
-   Episode 2
-   Episode 3
-   Episode 4
-   Episode 1
-   Episode 2
-   Episode 1

树视图重复所有数据.... :-( 你有什么想法吗?

1 个答案:

答案 0 :(得分:0)

我找到了解决方案

SqlCommand cmd;
cmd = new SqlCommand("S_fiction_program_search_v3", mycon);
cmd.CommandType = CommandType.StoredProcedure;
DataTable dt = new DataTable("treeProgram");
SqlDataAdapter dataAdp = new SqlDataAdapter(cmd);
dataAdp.Fill(dt);
DataSet ds = new DataSet();
ds.Tables.Add(dt);
ds.Relations.Add("rsParentChild", ds.Tables["treeProgram"].Columns["program_id"], ds.Tables["treeProgram"].Columns["program_header_id"]);
ds.Tables["treeProgram"].DefaultView.RowFilter = "program_header_id is null";
tvMain.ItemsSource = ds.Tables["treeProgram"].DefaultView;

ds.Tables [“treeProgram”]。DefaultView.RowFilter =“program_header_id为null”; 是神奇的线条!