Read csv file into wpf C#

public IEnumerable<Person> ReadCSV(string fileName)
{
    // We change file extension here to make sure it's a .csv file.
    // TODO: Error checking.
    string[] lines = File.ReadAllLines(System.IO.Path.ChangeExtension(fileName, ".csv"));

    // lines.Select allows me to project each line as a Person. 
    // This will give me an IEnumerable<Person> back.
    return lines.Select(line =>
    {
        string[] data = line.Split(';');
        // We return a person with the data in order.
        return new Person(data[0], data[1], Convert.ToInt32(data[2]), data[3]);
    });
}

4
5
Nietaki 95 points

                                    &lt;ListView x:Name=&quot;ListViewPeople&quot;&gt;
    &lt;ListView.View&gt;
        &lt;GridView&gt;
            &lt;GridViewColumn Header=&quot;First name&quot; Width=&quot;100&quot; DisplayMemberBinding=&quot;{Binding Path=FirstName}&quot;/&gt;
            &lt;GridViewColumn Header=&quot;Last name&quot; Width=&quot;150&quot; DisplayMemberBinding=&quot;{Binding Path=LastName}&quot;/&gt;
            &lt;GridViewColumn Header=&quot;ID&quot; Width=&quot;40&quot; DisplayMemberBinding=&quot;{Binding Path=ID}&quot;/&gt;
            &lt;GridViewColumn Header=&quot;Email&quot; Width=&quot;200&quot; DisplayMemberBinding=&quot;{Binding Path=Email}&quot;/&gt;
        &lt;/GridView&gt;
    &lt;/ListView.View&gt;
&lt;/ListView&gt;

4 (5 Votes)
0
4
6
Lieven 90 points

                                    public class Person
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public int ID { get; set; }
    public string Email { get; set; }

    public Person(string firstName, string lastName, int id, string email)
    {
        FirstName = firstName;
        LastName = lastName;
        ID = id;
        Email = email;
    }
}

4 (6 Votes)
0
4
2
Toscanelli 80 points

                                    public MainWindow()
{
    InitializeComponent();

    // We can access ListViewPeople here because that's the Name of our list
    // using the x:Name property in the designer.
    ListViewPeople.ItemsSource = ReadCSV(&quot;example&quot;);
}

4 (2 Votes)
0
Are there any code examples left?
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source