parser csv
-
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 }
-
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 }
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
toCsvLines
. 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
-
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
toCsvLines
. 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
thank you, tried it, but i can't debug solution when i add
CsvReaderViewModel vm = new CsvReaderViewModel("my source");
DataContext = vm;near InitializeComponent();