Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>There's a bunch that's wrong with the logic you've posted here. For one, each line in your array contains a string, because that's what <code>StreamReader.ReadLine()</code> returns. They may be strings that could conceivably be parsed into another data type, but they're not integers or decimals, they're strings. </p> <p>For another, your switch/case block is creating a new <code>PersonInfo</code> struct (and thus a row in the grid) for every element in the array. (I'm surprised by your assertion that the grid has the right number of rows; it looks to me like it should have 4x the number of rows you expect.) For yet another, you're adding the same four columns to your grid every time you process an element (fortunately, this doesn't cause an error, but it's unnecessary; the data grid only has four columns, not four columns for every row). And those columns each have a binding, but their binding has no source.</p> <p>You need to actually do very little of this. Creating bindings in code is the path of despair and misery.</p> <p>First, let's address parsing the text file. You haven't made clear how the file is formatted. A typical way to design a file format is so that each line represents an item (whether an object, a struct, a <code>DataRow</code>, or what have you), and the lines are separated into fields by delimiters. So a typical line that used vertical bars as delimiters might look like:</p> <pre><code>Abner Stoltzfus|36|1975-02-01|12000.00 </code></pre> <p>If you do this, you can split each line into an array, and then assign each field in your struct to an array element, e.g.:</p> <pre><code>List&lt;PersonInfo&gt; persons = new List&lt;personInfo&gt;(); 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); </code></pre> <p>That code probably has bugs in it - I'm writing it off the top of my head - so you'll need to fix it until when you run it, it reliably writes the person info to the Debug window. You should do this before you even begin screwing around with the WPF part of the problem.</p> <p>Pay attention to the things this code is doing that your code doesn't, like creating a collection to hold all of the <code>PersonInfo</code> structs, and parsing the strings into their native types. The last thing it does is take the <code>persons</code> list and adds it to the resource dictionary (I'm assuming here that this code is running in the code-behind of your <code>Window</code>; if not, there are plenty of other ways to do this).</p> <p>Now let's go to your XAML. I'm more familiar with <code>ListView</code> than <code>DataGrid</code>, so that's what I'm going to use in this example, but examples of how to do this with <code>DataGrid</code> abound.</p> <pre><code> &lt;ListView ItemsSource="{Binding {DynamicResource Persons}}"&gt; &lt;ListView.View&gt; &lt;GridView&gt; &lt;GridViewColumn Header="Name" DisplayMemberBinding="{Binding Name}"/&gt; &lt;GridViewColumn Header="Age" DisplayMemberBinding="{Binding Age}"/&gt; &lt;GridViewColumn Header="Date of birth" DisplayMemberBinding="{Binding DateOfBirth}"/&gt; &lt;GridViewColumn Header="Net worth" DisplayMemberBinding="{Binding NetWorth}"/&gt; &lt;/GridView&gt; &lt;/ListView.View&gt; &lt;/ListView&gt; &lt;/ListView&gt; </code></pre> <p>This does everything you were trying to do in your code and more: it creates the control, tells it where it will get its items from, tells it what columns to display, and defines the bindings for each columns.</p> <p>There are all kinds of reasons the above might not work, but now that the problem's segmented, at least you know where to look. If the Debug window doesn't contain the right data, then the logic that reads the data from the file has bugs in it. If the Output window contains binding errors, then there's something wrong (probably the field names) in the binding definitions in the XAML, or possibly the collection's not being added to the resource dictionary correctly.</p>
 

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