【问题标题】:Adding dynamically created columns and rows to listview wpf将动态创建的列和行添加到listview wpf
【发布时间】:2015-09-14 10:28:09
【问题描述】:

我正在尝试将动态列和动态行添加到列表视图。我只是不明白为什么这在 wpf 中很难做到。所以故事如下,我让用户从 SQL 数据库中选择一些表,例如 Table: PgPoints 和 PgValuesDouble。 所以列表视图应该将它的列标题显示为 PgPoints 和 PgValuesDouble。

接下来从 SQLDatabase 中获取选定表的名称(PgPoints 和 PgValuesDouble)的数据。此数据作为列表返回。

我现在有以下 C# 代码:

  public partial class Page3 : Page
{

    private List<string> columns = new List<string>();

    /*Variables passed through constructor*/
    private dbConnection connection;
    private List<string> selectedTables;


    public Page3(dbConnection connection, List<string> selectedTables)
    {
        InitializeComponent();
        this.connection = connection;
        this.selectedTables = selectedTables;
        init();
    }

    private void init()
    {
        gridView.Columns.Clear();

        //Loop through selected tables and add them as column names to listview
        foreach(string selectedTableNames in selectedTables)
        {
            GridViewColumn clm = new GridViewColumn();
            clm.Header = selectedTableNames;
            clm.Width = 100;
            gridView.Columns.Add(clm);

            //Loop through the data grabbed from the SQL database
            foreach (string columNames in connection.GetColumnsList(selectedTableNames))
            {
                columnListView.Items.Add(columNames);
            }
        }
    }

    private void button_Click(object sender, RoutedEventArgs e)
    {
        Button b = (Button)sender;

        if (b.Equals(button_back))
        {
            NavigationService.GoBack();
        }
        else
        {
            //Go forward event here!
        }
    }
}

还有 XAML:

<Page x:Class="HCCSVExport.Pages.Page3"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
  xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
  xmlns:local="clr-namespace:HCCSVExport.Pages"
  mc:Ignorable="d" 
  d:DesignHeight="300" d:DesignWidth="300"
  Title="Page3" MinWidth="519" MinHeight="321">

<Grid x:Name="grid" Background="White">
    <Button x:Name="button_next" Content="volgende" Margin="0,0,10,10" VerticalAlignment="Bottom" Click="button_Click" HorizontalAlignment="Right" Width="86.493"/>
    <Button x:Name="button_back" Content="vorige" Margin="0,0,101,10" VerticalAlignment="Bottom" Click="button_Click" HorizontalAlignment="Right" Width="89" Uid="button_back"/>
    <Label x:Name="label" Content="Selecteer de kolommen uit de tabellen" Margin="10,10,10,0" VerticalAlignment="Top" FontSize="16" HorizontalContentAlignment="Center"/>
    <ListView x:Name="columnListView" HorizontalAlignment="Left" Height="240" Margin="10,46,0,0" VerticalAlignment="Top" Width="499">
        <ListView.View>
            <GridView x:Name="gridView"/>
        </ListView.View>
    </ListView>
</Grid>

结果是从 SQL 数据库中抓取的所有数据都放入了两列。如何获取 PgPoints 列下属于 PgPoints 列的数据和 PgValuesDouble 列下属于 PgValuesDouble 列的数据?

Screenshot

如您所见,两列中的单元格是相同的,而不是每列的不同单元格...

编辑:

所以我已更改为似乎可以工作的数据网格。但我真的想要一个 ListView 来完成这项工作 :( 我只是不明白为什么在 wpf 中做到这一点有点令人沮丧......

public partial class Page3 : Page
{

    /*Variables passed through constructor*/
    private dbConnection connection;
    private List<string> selectedTables;

    public Page3(dbConnection connection, List<string> selectedTables)
    {
        InitializeComponent();
        this.connection = connection;
        this.selectedTables = selectedTables;
        init();
    }

    private void init()
    {
        dataGrid.ItemsSource = createDataTable().DefaultView;
    }

    private DataTable createDataTable()
    {
        DataTable dt = new DataTable();
        bool first = true;

        foreach (string selectedTableNames in selectedTables)
        {
            dt.Columns.Add(new DataColumn(selectedTableNames));

            List<string> tmpList = connection.GetColumnsList(selectedTableNames);

            for (int x = 0; x < tmpList.Count; x++)
            {
                if (first)
                {
                    DataRow row = dt.NewRow();
                    row[selectedTableNames] = tmpList[x];
                    dt.Rows.Add(row);
                }
                else
                {
                    DataRow row = dt.Rows[x];

                    if (row != null)
                    {
                        row[selectedTableNames] = tmpList[x];
                    }
                    else
                    {
                        row = dt.NewRow();
                        row[selectedTableNames] = tmpList[x];
                        dt.Rows.Add(row);
                    }


                }

                dt.AcceptChanges();
            }

            first = false;

            dt.AcceptChanges();


        }

        dt.AcceptChanges();
        return dt;
    }

    private void button_Click(object sender, RoutedEventArgs e)
    {
        Button b = (Button)sender;

        if (b.Equals(button_back))
        {
            NavigationService.GoBack();
        }
        else
        {
            //Go forward event here!
        }
    }
}

还有 XAML:

<Page x:Class="HCCSVExport.Pages.Page3"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
  xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
  xmlns:local="clr-namespace:HCCSVExport.Pages"
  mc:Ignorable="d" 
  d:DesignHeight="300" d:DesignWidth="300"
  Title="Page3" MinWidth="519" MinHeight="321">

<Grid x:Name="grid" Background="White">
    <Button x:Name="button_next" Content="volgende" Margin="0,0,10,10" VerticalAlignment="Bottom" Click="button_Click" HorizontalAlignment="Right" Width="86.493"/>
    <Button x:Name="button_back" Content="vorige" Margin="0,0,101,10" VerticalAlignment="Bottom" Click="button_Click" HorizontalAlignment="Right" Width="89" Uid="button_back"/>
    <Label x:Name="label" Content="Selecteer de kolommen uit de tabellen" Margin="10,10,10,0" VerticalAlignment="Top" FontSize="16" HorizontalContentAlignment="Center"/>
    <DataGrid x:Name="dataGrid" HorizontalAlignment="Left" Margin="10,46,0,0" VerticalAlignment="Top" Width="499" Height="240"/>
</Grid>

【问题讨论】:

  • 是 selectedTables 一个 List,这个方法是在后面的代码中定义的吗?
  • 嗨,Hardgraf,我用整个页面类更新了代码视图,所以它更容易阅读。如您所见, selectedTables 是一个 List
  • 目前还不清楚您要在行中添加什么。您有多少行以及需要显示哪些数据,例如在 row1 与 col1、row1 与 col2 等的 intersection 中。
  • 嗨,Ivan,行数无关紧要,因为它必须动态工作。我会上传一张截图,看看它现在的样子。

标签: c# wpf listview


【解决方案1】:

也许这会有所帮助,但我使用的是 DataGrid 而不是 ListView:

在 ViewModel 中我使用的是 DataTable:

DataTable dt = new DataTable();

DataColumn timeStamp = new DataColumn();
timeStamp.ColumnName = "TimeStamp";
dt.Columns.Add(timeStamp);

//ading a new row
DataRow row = dt.NewRow();

并填充它:

row[timeStamp] = "100.2";

以此类推,您可以添加许多具有特定值的列。

每列计数改变后:

LogTable.Clear();
LogTable = null;
LogTable = dt;

优雅与否,它对我来说效果很好,即使我动态添加超过 20 列,也不会出现任何故障。

 <DataGrid x:Name="logDataGrid"
           AutoGenerateColumns="True"
           ItemsSource="{Binding LogTable, Mode=TwoWay}"/>

我曾在某个时候看到过一个使用 ObservableCollection 而不是 DataTable 的示例,但这只是很多花哨的代码。

【讨论】:

  • 您好奥拉鲁,感谢您的建议!但是对于列表视图,这可悲的是:(
猜你喜欢
  • 1970-01-01
  • 2017-11-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-03-29
相关资源
最近更新 更多