gpt4 book ai didi

c# - ListView WPF 中的总和列

转载 作者:行者123 更新时间:2023-11-30 16:45:05 25 4
gpt4 key购买 nike

我有以下 csv 文件:

Name,Score
Kelly,5
James,5
Sara,1
Kelly,4
James,1
John,3

我正在尝试读取 CSV 文件,根据名称获取分数的总和并将它们加载到 ListView 中。

首先,我能够读取 CSV 文件并将其加载到每列的两个 ListView 中。下面是我的代码:

   public MainWindow()
{
InitializeComponent();
using (var fs = File.OpenRead(filepath))
using (var reader = new StreamReader(fs))
{
List<string> list0 = new List<string>();
List<string> list1 = new List<string>();
while (!reader.EndOfStream)
{
var line = reader.ReadLine();
var values = line.Split(',');

list0.Add(values[0]);
list1.Add(values[1]);
//sum = list1.Sum(x => Convert.ToInt32(x));
}

List0.ItemsSource = list0;
List1.ItemsSource = list1;

}
}

这是我使用的 xaml 代码:

    <ListView Name="List0" HorizontalAlignment="Left" Height="258" Margin="0,33,0,0" VerticalAlignment="Top" Width="267" >
<ListView.View>
<GridView>
<GridViewColumn DisplayMemberBinding="{Binding}"/>
</GridView>
</ListView.View>
</ListView>
<ListView Name="List1" HorizontalAlignment="Left" Height="258" Margin="272,33,0,0" VerticalAlignment="Top" Width="172" >
<ListView.View>
<GridView>
<GridViewColumn DisplayMemberBinding="{Binding}"/>
</GridView>
</ListView.View>
</ListView>

这对我来说效果很好。但是我从这里卡住了,我想知道是否有可能根据 ListView 中的名称获取分数列的总和。

比如获取Kelly的总分,获取James的总分等,listview如下所示:

Name,Score
Kelly,9
James,6
Sara,1
John,3

我尝试使用 Sum 选项并想将它打印到标签上进行测试,但它不起作用,我收到一条错误消息“输入字符串的格式不正确”

我在 C#、WPF 和 XAML 方面的经验还不够丰富;特别是在阅读和解析 CSV 文件时,所以我不确定如何去做。非常感谢您在此事或指点方面的任何帮助!谢谢!

最佳答案

我会创建一个新类来存储人员,包括他们的分数。像这样

public class Person
{
public string Name { get; private set; }
public int Score { get; set; }

public Person(string name, int score)
{
Name = name;
Score = Score;
}
}

然后你可以把你的逻辑改成这样

var lines = File.ReadAllLines(filepath);
var persons = new List<Person>();

foreach(var line in lines)
{
var values = line.Split(',');
var name = values[0];
var score = int.Parse(line[1].ToString());

var person = persons.FirstOrDefault(x => x.Name == name);
if(person == null)
{
var newPerson = new Person(name, score);
persons.Add(newPerson);
}
else
{
person.Score += score;
}
}

Person 类的优点是代码更清晰、更易理解;没有神奇的字符串,没有人知道它们的值意味着什么。

按照您的逻辑,当您的列表中已经有一个人的条目时,您可以提高该人的分数。

//编辑绑定(bind)到xaml您可以使用 DataGrid例如。

<DataGrid ItemsSource="{Binding Persons}" />

人是ObservableCollection<Person>在你的 View 模型中。 DataGrid将为您的 Person 类中的每个属性自动创建一列。您可以定制您的 DataGrid得到你想要的样子。网上有很多例子。

//编辑绑定(bind)与人的集合必须是公开的。否则您无法从 xaml 访问它。

你可以删除

var persons = new List<Person>();

并将其替换为您的 MainWindow 类中的一个属性。

public ObservableCollection<Person> Persons {get; set;}

你的类应该看起来像

public ObservableCollection<Person> Persons {get; set;}

public MainWindow()
{
InitializeComponent();

var lines = File.ReadAllLines(filepath);
Persons = new ObservableCollection<Person>();

在你的DataGrid将绑定(bind)更改为 ItemsSource="{Binding Persons}"

关于c# - ListView WPF 中的总和列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42807608/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com