Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. WPF
  4. parser csv

parser csv

Scheduled Pinned Locked Moved WPF
csharpcsswpf
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    MemberDotNetting
    wrote on last edited by
    #1

    Hi, i tried this code to parse CSV file and save data in a wpf data grid, the code is correct but i can't store values on datagrid this is a class wich contain the code: public class CsvParser { public List ReadAndParseData(string path, char separator) { var parseData = new List(); using (var sr = new StreamReader(path)) { string line; while ((line = sr.ReadLine()) != null) { string[] row = line.Split(separator); parseData.Add(row); } } return parseData; } } } the is the code behind; assigned to event click on boutton List parseData = csv.ReadAndParseData(datasource, ';'); foreach (string[] row in parseData) { MessageBox.Show(""+row.GetValue(0));// i get the correct value dgvreceipient.Items.Add(row.....);// but i get a empty datagrid }

    P 1 Reply Last reply
    0
    • M MemberDotNetting

      Hi, i tried this code to parse CSV file and save data in a wpf data grid, the code is correct but i can't store values on datagrid this is a class wich contain the code: public class CsvParser { public List ReadAndParseData(string path, char separator) { var parseData = new List(); using (var sr = new StreamReader(path)) { string line; while ((line = sr.ReadLine()) != null) { string[] row = line.Split(separator); parseData.Add(row); } } return parseData; } } } the is the code behind; assigned to event click on boutton List parseData = csv.ReadAndParseData(datasource, ';'); foreach (string[] row in parseData) { MessageBox.Show(""+row.GetValue(0));// i get the correct value dgvreceipient.Items.Add(row.....);// but i get a empty datagrid }

      P Offline
      P Offline
      Pete OHanlon
      wrote on last edited by
      #2

      Why do you persist in trying to do this the wrong way? You could solve this simply with an ObservableCollection and a little bit of binding.

      public class CsvReaderViewModel
      {
      public ObservableCollection<string> CsvLines { get; set; }

      public CsvReaderViewModel(string datasource)
      {
      List<string> csv = new CsvParser().ReadAndParseData(datasource, ';');
      CsvLines = new CsvLines(csv);
      }

      }

      Then, "in your code behind", all you need do is (make sure you have called InitializeComponent before you try this):

      CsvReaderViewModel vm = new CsvReaderViewModel("my source");
      DataContext = vm;

      Finally, you just need to set your grid ItemsSource to CsvLines. BTW - you have posted and been told often enough, format your code blocks using the pre tags. It's not hard to do.

      *pre-emptive celebratory nipple tassle jiggle* - Sean Ewington

      "Mind bleach! Send me mind bleach!" - Nagy Vilmos

      CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easier

      M 1 Reply Last reply
      0
      • P Pete OHanlon

        Why do you persist in trying to do this the wrong way? You could solve this simply with an ObservableCollection and a little bit of binding.

        public class CsvReaderViewModel
        {
        public ObservableCollection<string> CsvLines { get; set; }

        public CsvReaderViewModel(string datasource)
        {
        List<string> csv = new CsvParser().ReadAndParseData(datasource, ';');
        CsvLines = new CsvLines(csv);
        }

        }

        Then, "in your code behind", all you need do is (make sure you have called InitializeComponent before you try this):

        CsvReaderViewModel vm = new CsvReaderViewModel("my source");
        DataContext = vm;

        Finally, you just need to set your grid ItemsSource to CsvLines. BTW - you have posted and been told often enough, format your code blocks using the pre tags. It's not hard to do.

        *pre-emptive celebratory nipple tassle jiggle* - Sean Ewington

        "Mind bleach! Send me mind bleach!" - Nagy Vilmos

        CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easier

        M Offline
        M Offline
        MemberDotNetting
        wrote on last edited by
        #3

        thank you, tried it, but i can't debug solution when i add

        CsvReaderViewModel vm = new CsvReaderViewModel("my source");
        DataContext = vm;

        near InitializeComponent();

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups