【发布时间】:2018-03-19 00:08:20
【问题描述】:
DataTable dt;
我在使用 DataTable 时遇到问题。我有一个double[] 列表,但它拒绝显示最后一列的值。
for (int j = 0; j < header.Count; j++)
{
dt.Columns.Add(header[j]);
}
dt.Columns.Add("Extra");
if ((dataIn as List<double[]>) != null)
{
// dataIn = a list of 2 arrays of 3.
localDataInDouble = dataIn as List<double[]>;
//Add rows to table
for (int j = 0; j < localDataInDouble.Count; j++)
{
workRow = dt.NewRow();
for (int i = 0; i < header.Count; i++)
{
workRow[header[i]] = localDataInDouble[j][i];
}
dt.Rows.Add(workRow);
}
dt.Rows[0][2] = "hello";
dt.Rows[0][3] = 2.55;
dt.Rows[1][2] = 2.55;
dt.Rows[1][3] = 2.55;
}
this.csvContent.ItemsSource = this.dt.DefaultView;
foreach (DataRowView v in this.csvContent.Items)
{
var row = v.Row;
foreach (object o in row.ItemArray)
{
Console.Write(o + ";");
}
Console.WriteLine();
}
如您所见,DataTable 的值设置正确,但不会在DataTable 本身中显示它们。
我尝试添加一个“额外”列,希望它只是最后一列,然后隐藏额外的列,但这也不是正在发生的事情。
我还手动在DataRow 中设置了一些数据,因为它与 Extra 行一起使用。
如果我在csv 文件中添加另一列“Toff”会显示其值,但我在csv 文件/inputList 中添加的列没有值。
非常感谢您的帮助。
提前致谢。
编辑:我还尝试在标题列表中添加 Extra 列,并在 inputList 中添加额外值。这也不起作用。
编辑 2:XAML
<ScrollViewer Name="topLevel" VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Auto" >
<Grid ScrollViewer.VerticalScrollBarVisibility="Auto" ScrollViewer.HorizontalScrollBarVisibility="Auto" VerticalAlignment="Top" HorizontalAlignment="Center" >
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="100" />
</Grid.RowDefinitions>
<DataGrid Name="csvContent" Grid.Column="0" Grid.Row="0" Margin="10,10" CanUserAddRows="False" ItemsSource="{Binding Path=DataContext, RelativeSource={RelativeSource Self}, Mode=TwoWay }" />
【问题讨论】:
-
您是否尝试将 ListView 显示的列放大一点?数据存在于表中,所以问题只是一个可视化问题
-
@Steve 是的。但它仍然没有显示出来。添加了一张新图片。
-
试图设置一个数字而不是“你好”?
-
@Steve 也这样做了 :(
-
从:header.Count 更改为 dt.Columns.Count。看起来您创建了一个包含一列 Cycle 的数据表。然后您使用 header.Count 添加了两列。所以标题数组有两列,数据表有三列。当您将行添加到 xaml 时,您只添加了两列而不是三列。
标签: c# wpf gridview data-binding datatable