C# 需要有关StreamReader、ArrayList和DataGrid协同工作的帮助吗
我正在做一个基本上是账单提醒的程序,主要是在自学C#和WPF之后的第一个程序。我可以让用户输入详细信息,然后将其保存到文本文件中。我现在正试图对稍后从文本加载文件的部分进行编码。我使用StreamReader将文本文件读入ArrayList,然后让程序遍历ArrayList并填充DataGrid。但有些东西不起作用,我最终得到了一个空白的数据网格,但行数和列标题都正确。我认为问题在于,我正在使用开关大小写来确定每个ArrayList位置的类型,然后将该位置的内容放在正确的列中,但是StreamReader将所有内容作为字符串拉入,从而使开关大小写毫无意义 因此,基本上我的问题是,如何让StreamReader将项目放置在正确的值类型中,或者这就是问题所在 这就是我在玩的代码。虽然它实际上不是预算项目,但我只是将其用作测试平台,所以我不会“污染”我的好代码这些值是完全相同的值类型,所以一旦它工作并且我将其传输过来,它将完成我所需要的一切C# 需要有关StreamReader、ArrayList和DataGrid协同工作的帮助吗,c#,.net,wpf,wpfdatagrid,streamreader,C#,.net,Wpf,Wpfdatagrid,Streamreader,我正在做一个基本上是账单提醒的程序,主要是在自学C#和WPF之后的第一个程序。我可以让用户输入详细信息,然后将其保存到文本文件中。我现在正试图对稍后从文本加载文件的部分进行编码。我使用StreamReader将文本文件读入ArrayList,然后让程序遍历ArrayList并填充DataGrid。但有些东西不起作用,我最终得到了一个空白的数据网格,但行数和列标题都正确。我认为问题在于,我正在使用开关大小写来确定每个ArrayList位置的类型,然后将该位置的内容放在正确的列中,但是StreamR
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
ArrayList array = new ArrayList();
DataGridTextColumn nameOfPerson = new DataGridTextColumn();
nameOfPerson.Binding = new Binding("name");
DataGridTextColumn ageOfPerson = new DataGridTextColumn();
ageOfPerson.Binding = new Binding("age");
DataGridTextColumn birthdayOfPerson = new DataGridTextColumn();
birthdayOfPerson.Binding = new Binding("birthday");
DataGridTextColumn netWorth = new DataGridTextColumn();
netWorth.Binding = new Binding("netWorth");
using (StreamReader reader = new StreamReader("ArrayListSource.txt"))
{
while (!reader.EndOfStream)
{
array.Add(reader.ReadLine());
}
}
//Array is now populated with contents of text file.
string name;
int age;
DateTime birthday;
decimal value;
//Checks the type of each entry in the array.
for (int i = 0; i < array.Count; i++)
{
System.Type idType = array[i].GetType();
string currentItem = idType.Name;
switch (currentItem)
{
case "String":
name = (string)array[i];
dataGrid1.Items.Add(new PersonInfo() { nameOfPerson = name });
break;
case "Int32":
age = (int)array[i];
dataGrid1.Items.Add(new PersonInfo() { ageOfPerson = age });
break;
case "DateTime":
birthday = (DateTime)array[i];
dataGrid1.Items.Add(new PersonInfo() { birthdayOfPerson = birthday });
break;
case "Decimal":
value = (decimal)array[i];
dataGrid1.Items.Add(new PersonInfo() { netWorth = value });
break;
}
dataGrid1.Columns.Add(nameOfPerson);
dataGrid1.Columns.Add(ageOfPerson);
dataGrid1.Columns.Add(birthdayOfPerson);
dataGrid1.Columns.Add(netWorth);
nameOfPerson.Header = "Name";
ageOfPerson.Header = "Age";
birthdayOfPerson.Header = "Birthdate";
netWorth.Header = "Net Worth";
}
public struct PersonInfo
{
public string nameOfPerson { get; set; }
public int ageOfPerson { get; set; }
public DateTime birthdayOfPerson { get; set; }
public decimal netWorth { get; set; }
}
}
公共部分类主窗口:窗口
{
公共主窗口()
{
初始化组件();
ArrayList数组=新的ArrayList();
DataGridTextColumn nameOfPerson=新DataGridTextColumn();
name of person.Binding=新绑定(“名称”);
DataGridTextColumn ageOfPerson=新建DataGridTextColumn();
年龄绑定=新绑定(“年龄”);
DataGridTextColumn birthdayOfPerson=新DataGridTextColumn();
birthdayOfPerson.Binding=新绑定(“生日”);
DataGridTextColumn netWorth=新DataGridTextColumn();
netWorth.Binding=新绑定(“netWorth”);
使用(StreamReader=newstreamreader(“ArrayListSource.txt”))
{
而(!reader.EndOfStream)
{
Add(reader.ReadLine());
}
}
//数组现在由文本文件的内容填充。
字符串名;
智力年龄;
日期时间生日;
十进制值;
//检查数组中每个项的类型。
for(int i=0;i
无论如何,不要,不要在2011年使用ArrayList!改用genericList
。或者只使用数组:
string[] lines = System.IO.File.ReadAllLines("ArrayListSource.txt");
就我个人而言,我会转课:
public class PersonInfo { }
无论如何,不要,不要在2011年使用ArrayList!改用generic
List
。或者只是一个数组:
string[] lines = System.IO.File.ReadAllLines("ArrayListSource.txt");
就我个人而言,我会转课:
public class PersonInfo { }
你在这里发布的逻辑有很多错误。首先,数组中的每一行都包含一个字符串,因为
StreamReader.ReadLine()
返回的是字符串。它们可能是可以解析为其他数据类型的字符串,但它们不是整数或小数,而是字符串
另一方面,您的switch/case块正在为数组中的每个元素创建一个新的PersonInfo
结构(从而在网格中创建一行)。(您断言网格的行数正确,我对此感到惊讶;在我看来,它的行数应该是您期望的4倍。)另一方面,每次处理元素时,都要向网格中添加相同的四列(幸运的是,这不会导致错误,但这是不必要的;数据网格只有四列,而不是每行四列)。这些列都有绑定,但它们的绑定没有源
实际上,你只需要做很少的事情。在代码中创建绑定是一条绝望和痛苦的道路
首先,让我们来分析文本文件。您还没有弄清楚文件的格式。设计文件格式的一种典型方法是让每一行代表一个项(无论是对象、结构、数据行还是您所拥有的),并用分隔符将行分隔为字段。因此,使用垂直条作为分隔符的典型行可能如下所示:
Abner Stoltzfus|36|1975-02-01|12000.00
如果执行此操作,可以将每行拆分为一个数组,然后将结构中的每个字段指定给一个数组元素,例如:
List<PersonInfo> persons = new List<personInfo>();
using (StreamReader sr = new StreamReader(filename))
{
while (!sr.EndOfStream)
{
string line = sr.ReadLine();
string[] fields = line.Split(new char[] { '|' });
PersonInfo p = new PersonInfo
{
Name = fields[0],
Age = int.Parse(fields[1]),
DateOfBirth = DateTime.Parse(fields[2]),
NetWorth = decimal.Parse(fields[3])
});
Debug.WriteLine(string.Format("Name={0}, Age={1}, DateOfBirth={2}, NetWorth={3}",
p.Name, p.Age, p.DateOfBirth, p.NetWorth);
persons.Add(p);
}
}
AddResource("Persons", persons);
这完成了您试图在代码中执行的所有操作,以及更多操作:它创建控件,告诉它从何处获取项目,告诉它要创建哪些列