0

I am using a WPF application. I am new to wpf but I have experience with Windows Forms. Wpf is great I want to learn how to programme with it. My problem is: I have written a code, I can use it but its not possible for me to use the OpenFileDialog function correctly. I want to read a csv txt file, structure of files are always the same only the rows are different. The name of the files is also different, I want to read and edit one file after the other. Read, edit, save. My Code is here:

public static class PersonService

public static List<Person> ReadFile(string filepath)
{
    var lines = File.ReadAllLines(filepath);

    var data = from l in lines.Skip(1)
               let split = l.Split(';')
               select new Person
               {
                   Id = int.Parse(split[0]),
                   Name = split[1],
                   Age = int.Parse(split[2]),
                   Gender = (Gender)Enum.Parse(typeof(Gender), split[3])
               };

    return data.ToList();
}

}

public partial class Window2 : Window

public Window2()
{
    InitializeComponent();

OpenFileDialog csv = new OpenFileDialog DataContext = PersonService.ReadFile(csv);

<Window x:Class="WpfApplication14.Window2"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window2" Height="300" Width="300">
<DataGrid AutoGenerateColumns="True"
          ItemsSource="{Binding}"/>

Dmitry
  • 2,033
  • 1
  • 22
  • 31
DOEJD
  • 1
  • 1
  • 2
  • I would say it is a duplicate to [this](https://stackoverflow.com/questions/10315188/open-file-dialog-and-select-a-file-using-wpf-controls-and-c-sharp), however the Android tag on the current question is bit confusing. – Congenital Optimist Jan 06 '19 at 14:37
  • Possible duplicate of [Open file dialog and select a file using WPF controls and C#](https://stackoverflow.com/questions/10315188/open-file-dialog-and-select-a-file-using-wpf-controls-and-c-sharp) – Wayne Phipps Jan 06 '19 at 16:20

1 Answers1

0

This should look something like this:

//Create file dialog and configure to open csv files only
OpenFileDialog csvFielDialog = new OpenFileDialog();
csvFielDialog.Filter = "CSF files(*.csv)|*.csv";

//Show the dialog to the user
bool? result = csvFielDialog.ShowDialog();

//If the user selected something perform the actions with the file
if(result.HasValue && result.Value)
{
    DataContext = PersonService.ReadFile(csvFielDialog.FileName);
}
Dmitry
  • 2,033
  • 1
  • 22
  • 31