gpt4 book ai didi

c# - 将动态创建的列和行添加到 ListView wpf

转载 作者:太空宇宙 更新时间:2023-11-03 15:33:58 24 4
gpt4 key购买 nike

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

最佳答案

也许这会有所帮助,不过我使用的是 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 的示例,但它只是很多花哨的代码。

关于c# - 将动态创建的列和行添加到 ListView wpf,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32562554/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com