Note that there are some explanatory texts on larger screens.

plurals
  1. PONeed help with StreamReader, ArrayList, and DataGrid working together
    text
    copied!<p>I am working on a program that is basically a bill reminder, mostly for a first program after having taught myself C# &amp; WPF. I am able to have the user enter details, then save it to a text file. I am now trying to code the part which later loads a file from text. I used StreamReader to read the text file into an ArrayList, then have the program iterate through the ArrayList and populate the DataGrid. Something isn't working though, and I'm ending up with a blank DataGrid, but with the correct number of rows and properly headed columns. I believe the problem is that I'm using a switch-case to determine the type of each ArrayList location, then place the contents of that location in the correct column, but StreamReader pulls everything in as a string, thus making the switch-case pointless.</p> <p>So basically my question is, how do I make StreamReader place the items in the correct value type, or is that even the problem?</p> <p>This is the code I'm playing with. Although it's not actually the budget program, I've just been using this as a test bed so I don't "contaminate" my good code. :) These values are exactly the same value types though, so it will do everything I need it to once it works and I transfer it over.</p> <pre><code>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 &lt; 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; } } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload