我是C#的新手, CSV数据无法在DataGrid的WPF ui上呈现,我正在从csv获取数据,但无法在ui上显示,有人可以帮助我吗?
下面是我的XAML代码-
ID Itemdescription Date
1 Lenovo 4/9/2014
2 Null null
Update1 : 下面是C#代码:
<Window x:Class="SampleApp.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"
xmlns:local="clr-namespace:SampleApp"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="825">
<DataGrid AutoGenerateColumns="True" Height="217" HorizontalAlignment="Left" Margin="73,33,0,0" Name="dataGrid1" VerticalAlignment="Top" Width="707">
<DataGrid.Columns>
<DataGridTextColumn Header="Serials" Width="40" Binding="{Binding Serials}"></DataGridTextColumn>
<DataGridTextColumn Header="Number" Width="100" Binding="{Binding Number}"></DataGridTextColumn>
<DataGridTextColumn Header="InstalledCode" Width="100" Binding="{Binding InstalledCode}"></DataGridTextColumn>
<DataGridTextColumn Header="InstalledTime" Width="100" Binding="{Binding InstalledTime}"></DataGridTextColumn>
<DataGridTextColumn Header="Location" Width="100" Binding="{Binding Location}"></DataGridTextColumn>
<DataGridTextColumn Header="Site" Width="40" Binding="{Binding Site}"></DataGridTextColumn>
<DataGridTextColumn Header="UUID" Width="40" Binding="{Binding UUID}"></DataGridTextColumn>
<DataGridTextColumn Header="Uninstalled" Width="100" Binding="{Binding Uninstalled}"></DataGridTextColumn>
<DataGridTextColumn Header="Notes" Width="100" Binding="{Binding Notes}"></DataGridTextColumn>
</DataGrid.Columns>
</DataGrid>
</Window>
Update2:
我的Csv文件是
private void LoadBound(string fName)
{
String textLine = string.Empty;
String[] splitLine;
var data = new DataTable();
var sharath = new DataTable();
data.Columns.AddRange(new[]
{
new DataColumn ("Serials"),
new DataColumn ("Number"),
new DataColumn ("InstalledCode"),
new DataColumn ("InstalledTime"),
new DataColumn ("Location"),
new DataColumn ("Site"),
new DataColumn ("UUID"),
new DataColumn ("Uninstalled"),
new DataColumn ("Notes")
});
data.Rows.Clear();
if (System.IO.File.Exists(fName))
{
System.IO.StreamReader objReader = new System.IO.StreamReader(fName);
var contents = objReader.ReadToEnd();
var strReader = new System.IO.StringReader(contents);
do
{
textLine = strReader.ReadLine();
if (textLine != string.Empty)
{
splitLine = textLine.Split(',');
if (splitLine[0] != string.Empty || splitLine[1] != string.Empty)
{
data.Rows.Add(splitLine[0]);
}
}
} while (strReader.Peek() != -1);
}
dataGrid1.DataContext = data;
}
有人可以帮我从csv向WPF Ui填充数据吗?
答案 0 :(得分:2)
您还没有设置DataGrid.ItemsSource。
如果DataContext设置为DataTable,则可以通过在xaml中进行绑定来设置ItemsSource:
ItemsSource="{Binding Path=DefaultView}"
或者通过代码设置ItemsSource:
dataGrid1.ItemsSource = data.DefaultView;