기본적으로 C# & WPF를 가르친 후 첫 번째 프로그램에 대한 청구서 알림 프로그램입니다. 사용자가 세부 정보를 입력 한 다음 텍스트 파일에 저장할 수 있습니다. 지금은 나중에 텍스트에서 파일을로드하는 부분을 코딩하려고합니다. StreamReader를 사용하여 텍스트 파일을 ArrayList로 읽은 다음 프로그램에서 ArrayList를 반복하고 DataGrid를 채 웁니다. 뭔가가 작동하지 않습니다, 그리고 난 빈 DataGrid로 끝날거야하지만 올바른 행 번호와 제대로 향하고 열. 문제는 내가 각 ArrayList 위치의 유형을 결정하기 위해 switch-case를 사용하고 그 위치의 내용을 올바른 열에 배치하지만 StreamReader가 모든 것을 문자열로 가져 와서 스위치 케이스를 만드는 것이라고 생각합니다. 무의미한.StreamReader, ArrayList 및 DataGrid가 함께 작업하는 데 도움이 필요합니다.
그래서 기본적으로 내 질문은 어떻게 StreamReader 항목을 올바른 값 형식으로 배치합니까 또는 심지어 문제가 무엇입니까?
이것은 내가 가지고 놀고있는 코드입니다. 실제로 예산 프로그램은 아니지만 테스트 코드로이 코드를 사용하고 있으므로 좋은 코드를 "오염"시키지 않습니다. :) 그러나이 값은 정확히 동일한 값 유형이므로, 일단 작동하면 필요로하는 모든 작업을 수행 할 것입니다.
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; }
}
}
WPF DataGrid를 처음으로 사용하려고하십니까? 너는 용감하다. :) –
나는 그것을 깨닫게 될 것이다. 그러나 그것이 내가 원하는 것을한다. :) –