DataTable中如何横向显示List<int>?
How to display List<int> in DataTable horizontally?
这个问题与有关。在上一个问题中,感谢@EldHasp,我能够在 DataGrid
.
中显示任意多个 List<int>
的元素
现在,我想在 DataTable 中做同样的事情。
我已经试过了;然而,它只显示 System.Collections.Generic.List 1[System.In32]
。
我确定我错了,但我不知道如何解决这个问题。
虽然我认为 AutoGenerateColumns="True"
会自动修复此问题,但它并没有发生。
这是我的代码:
CatModel.cs(除注释外与上一个相同)
using System;
using System.Collections.Generic;
namespace WpfApp1
{
public class CatModel
{
public CatModel(int Num, String Name, List<int> Test_List)
{
this.Num = Num;
this.Name = Name;
this.Test_List = Test_List;
}
public int Num { get; set; }
public String Name { get; set; }
public List<int> Test_List { get; set; }
}
}
MainWindow.xaml.cs
using System.Collections.Generic;
using System.Data;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
namespace WpfApp1
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
DataTable result = new DataTable();
result.Columns.Add("Num");
result.Columns.Add("Name");
for (int i = 0; i < 3; i++)
{
result.Columns.Add("Test_" + (i + 1));
}
result.Rows.Add(1, "AA", new List<int> { 1, 2, 3 });
result.Rows.Add(2, "BB", new List<int> { 4, 5, 6 });
result.Rows.Add(3, "CC", new List<int> { 7, 8, 9 });
DataContext = result;
}
}
}
MainWindow.xaml
<Window x:Class="WpfApp1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Title="List" Height="350" Width="750"
BorderThickness="1">
<Grid Width="700" Height="300">
<DataGrid IsReadOnly="True" AutoGenerateColumns="True" ItemsSource="{Binding}" HorizontalAlignment="Left" Margin="10,10,10,10"/>
</Grid>
</Window>
可能的实施示例:
public DataTable Table { get; } = new DataTable();
public MainWindow()
{
List<CatModel> result = new List<CatModel>();
result.Add(new CatModel(1, "AA", new List<int> { 1, 2, 3 }));
result.Add(new CatModel(2, "BB", new List<int> { 4, 5, 6 }));
result.Add(new CatModel(3, "CC", new List<int> { 7, 8, 9 }));
Table.Columns.Add(new DataColumn(nameof(CatModel.Num), typeof(int)));
Table.Columns.Add(new DataColumn(nameof(CatModel.Name), typeof(string)));
for (int i = 0; i < 3; i++)
{
Table.Columns.Add(new DataColumn ($"{i}", typeof(int)));
}
foreach (var cat in result)
{
var row = Table.NewRow();
row[0] = cat.Num;
row[1] = cat.Name;
for (int i = 0; i < 3; i++)
{
row[2 + i] = cat.Test_List[i];
}
Table.Rows.Add(row);
}
InitializeComponent();
}
<Window x:Class="WpfApp1.MainWindow"
x:Name="mainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Title="List" Height="350" Width="750"
BorderThickness="1">
<Grid Width="700" Height="300">
<DataGrid IsReadOnly="True" AutoGenerateColumns="True"
ItemsSource="{Binding Table, ElementName=mainWindow}"
HorizontalAlignment="Left" Margin="10,10,10,10"/>
</Grid>
</Window>
P.S.结果合集多余.
我留下它只是因为与问题中的代码有联系,这样可以更清楚table单元格如何与原始数据链接。
最好在没有中间集合的情况下立即初始化 table。
这个问题与DataGrid
.
中显示任意多个 List<int>
的元素
现在,我想在 DataTable 中做同样的事情。
我已经试过了;然而,它只显示 System.Collections.Generic.List 1[System.In32]
。
我确定我错了,但我不知道如何解决这个问题。
虽然我认为 AutoGenerateColumns="True"
会自动修复此问题,但它并没有发生。
这是我的代码:
CatModel.cs(除注释外与上一个相同)
using System;
using System.Collections.Generic;
namespace WpfApp1
{
public class CatModel
{
public CatModel(int Num, String Name, List<int> Test_List)
{
this.Num = Num;
this.Name = Name;
this.Test_List = Test_List;
}
public int Num { get; set; }
public String Name { get; set; }
public List<int> Test_List { get; set; }
}
}
MainWindow.xaml.cs
using System.Collections.Generic;
using System.Data;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
namespace WpfApp1
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
DataTable result = new DataTable();
result.Columns.Add("Num");
result.Columns.Add("Name");
for (int i = 0; i < 3; i++)
{
result.Columns.Add("Test_" + (i + 1));
}
result.Rows.Add(1, "AA", new List<int> { 1, 2, 3 });
result.Rows.Add(2, "BB", new List<int> { 4, 5, 6 });
result.Rows.Add(3, "CC", new List<int> { 7, 8, 9 });
DataContext = result;
}
}
}
MainWindow.xaml
<Window x:Class="WpfApp1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Title="List" Height="350" Width="750"
BorderThickness="1">
<Grid Width="700" Height="300">
<DataGrid IsReadOnly="True" AutoGenerateColumns="True" ItemsSource="{Binding}" HorizontalAlignment="Left" Margin="10,10,10,10"/>
</Grid>
</Window>
可能的实施示例:
public DataTable Table { get; } = new DataTable();
public MainWindow()
{
List<CatModel> result = new List<CatModel>();
result.Add(new CatModel(1, "AA", new List<int> { 1, 2, 3 }));
result.Add(new CatModel(2, "BB", new List<int> { 4, 5, 6 }));
result.Add(new CatModel(3, "CC", new List<int> { 7, 8, 9 }));
Table.Columns.Add(new DataColumn(nameof(CatModel.Num), typeof(int)));
Table.Columns.Add(new DataColumn(nameof(CatModel.Name), typeof(string)));
for (int i = 0; i < 3; i++)
{
Table.Columns.Add(new DataColumn ($"{i}", typeof(int)));
}
foreach (var cat in result)
{
var row = Table.NewRow();
row[0] = cat.Num;
row[1] = cat.Name;
for (int i = 0; i < 3; i++)
{
row[2 + i] = cat.Test_List[i];
}
Table.Rows.Add(row);
}
InitializeComponent();
}
<Window x:Class="WpfApp1.MainWindow"
x:Name="mainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Title="List" Height="350" Width="750"
BorderThickness="1">
<Grid Width="700" Height="300">
<DataGrid IsReadOnly="True" AutoGenerateColumns="True"
ItemsSource="{Binding Table, ElementName=mainWindow}"
HorizontalAlignment="Left" Margin="10,10,10,10"/>
</Grid>
</Window>
P.S.结果合集多余.
我留下它只是因为与问题中的代码有联系,这样可以更清楚table单元格如何与原始数据链接。
最好在没有中间集合的情况下立即初始化 table。