CSV数据无法在DataGrid的WPF ui上呈现[重复]

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了CSV数据无法在DataGrid的WPF ui上呈现[重复]相关的知识,希望对你有一定的参考价值。

这个问题在这里已有答案:

我是C#的新手,CSV数据无法在DataGrid的WPF ui上呈现,我从csv获取数据但无法在ui上显示,是否可以帮助我?

以下是我的XAML代码 -

<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>

Update1:​​下面是c#代码:

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;
        }

UPDATE2:

我的Csv文件是

10,20,30,40,50,60,70,80
1,2,3,4,5,6,7,8

任何人都可以帮我填充从csv到WPF Ui的数据吗?

答案

你没有设置DataGrid.ItemsSource。

如果DataContext设置为DataTable,则可以通过xaml中的绑定设置ItemsSource:

ItemsSource="{Binding Path=DefaultView}"

或者从代码中设置ItemsSource:

dataGrid1.ItemsSource = data.DefaultView;

以上是关于CSV数据无法在DataGrid的WPF ui上呈现[重复]的主要内容,如果未能解决你的问题,请参考以下文章

wpf - 寻找在归档datagrid时不冻结ui的方法

WPF中的DataGrid 数据动态刷新UI

WPF DataGrid - 如何暂停数据绑定中的 UI 更新并稍后进行批量更新

WPF Datagrid:通过已使用 ValueConverter 转换的 UI 编辑值 [关闭]

如何在 WPF 中设置 DataGrid 的 DataSource?

WPF DataGrid 全行编辑