首页 > 解决方案 > 如何将值从文本文件传递到 DataGrid?C#WPF

问题描述

我正在使用 wpf 在 C# 中编写一个简单的程序,类似于 base,我知道使用 subd 和实体框架解决这个问题会更容易,但关键是你需要这样解决

所以,我有一个文本文件,我需要从中将数据加载到日期网格中。

我有一个类描述了这个文本文件中的一行,这里是:

public class Student
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string LastName { get; set; }
    public string MidName { get; set; }
    public int Informatics { get; set; }
    public int Maths { get; set; }
    public int Physics { get; set; }
    public double Score { get; set; }
}

分数字段,用于来自程序的数据(学生成绩的算术平均值)

我有一个数据网格,我需要在其中输出数据:

<DataGrid x:Name="DGridStudents" IsReadOnly="True" AutoGenerateColumns="False" HorizontalAlignment="Left" Height="329" Margin="57,15,0,0" VerticalAlignment="Top" Width="736">
        <DataGrid.Columns>
            <DataGridTextColumn Header="ID" Width="*" Binding="{Binding Id}"/>
            <DataGridTextColumn Header="Name" Width="*" Binding="{Binding Name}"/>
            <DataGridTextColumn Header="LastName" Width="*" Binding="{Binding LastName}"/>
            <DataGridTextColumn Header="MidName" Width="*" Binding="{Binding MidName}"/>
            <DataGridTextColumn Header="Informatics" Width="*" Binding="{Binding Informatics}"/>
            <DataGridTextColumn Header="Maths" Width="*" Binding="{Binding Maths}"/>
            <DataGridTextColumn Header="Physics" Width="*" Binding="{Binding Physics}"/>
            <DataGridTextColumn Header="Score" Width="*" Binding="{Binding Score}"/>
        </DataGrid.Columns>
    </DataGrid>

因此,我曾经填写 DataGrid,现在我需要添加 Score 字段(科目成绩的算术平均值)

List<Student> list = new List<Student>();
            using (StreamReader sr = new StreamReader(fileName, true))
            {
                
                string line;
                while ((line = sr.ReadLine()) != null)
                {
                    var parsed = line.Split(' ');
                    list.Add(new Student
                        (
                           Convert.ToInt32(parsed[0]),
                           parsed[1],
                           parsed[2],
                           parsed[3],
                           Convert.ToInt32(parsed[4]),
                           Convert.ToInt32(parsed[5]),
                           Convert.ToInt32(parsed[6])
                        ));
                }
            }
            DGridStudents.ItemsSource = list;

请告诉我如何在不使用 MVVM 模式的情况下使用数据绑定来做到这一点?

文本文件中的一行示例:

1本尼迪克特蒂莫西卡尔顿康伯巴奇5 5 5

标签: c#wpfdatagridtxt

解决方案


var student = new Student
                        {
                            Id = Convert.ToInt32(parsed[0]),
                            Name = parsed[1],
                            LastName = parsed[2],
                            MidName = parsed[3],
                            Informatika = Convert.ToInt32(parsed[4]),
                            Matematika = Convert.ToInt32(parsed[5]),
                            Fizika = Convert.ToInt32(parsed[6]),
                            Score = 5
                        };
                        list.Add(student);

推荐阅读