具有存储过程的 C# 树视图

本文关键字:视图 存储过程 | 更新日期: 2023-09-27 18:32:46

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

该过程返回一个系列及其季节和剧集。

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

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

具有存储过程的 C# 树视图

我找到了解决方案

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.表["树程序"]。DefaultView.RowFilter = "program_header_id is null";是魔线!