2017-09-05 119 views
0

我目前正在编写一个WPF C#中的应用程序,它是用Java编写的其他进程的某种帮助器。那些其他进程需要一个configuration.csv,其中列出了不同的“名称”和一个名为“SKIP”的列。如果该列在跳过时是X,那么我的java程序将跳过这些名称,从而忽略它们的依赖进程。C#WPF读取/编辑CSV

如果我用Excel打开CSV并编辑行,一切正常。这不是问题。我想要实现的是将行列入WPF应用程序中的DataGrid(第一行和最后一行除外),其中用户可以勾选复选框以决定是否要跳过该特定名称。通过按保存,.CSV得到更新。

我已经写了一些代码与一个朋友谁更熟悉这个话题。它在WinForms中运行良好,但不适用于WPF。我们无法获得复选框的值,也无法将它们保存到CSV中。

CODE:

private void OBJ_SaveButton_Click(object sender, RoutedEventArgs e) 
    { 
     if (OBJ_DataGrid.Items.Count == 0) 
     { 
      MessageBox.Show("Kein Datensatz in der View."); 
      return; 
     } 
     /*if (Directory.Exists(path)) 
     { 
      if (File.Exists(filepath)) 
      { 
       string tmp = null; 
       try 
       { 
        FileStream fileStr = new FileStream(filepath, FileMode.Create); 
        StreamWriter strWriter = new StreamWriter(fileStr); 
        strWriter.WriteLine("SFObject;Skip"); 
        for(int i=0;i< itmGrd.Count;i++) 
        { 
         switch (itmGrd[i].ItemValue) 
         { 
          case true: 
           tmp = itmGrd[i].ItemName + ";X"; 
           break; 
          case false: 
           tmp = itmGrd[i].ItemName + ";"; 
           break; 
         } 
         strWriter.WriteLine(tmp); 
        } 
        strWriter.WriteLine("SuccessMSG;"); 
        strWriter.Close(); 
        fileStr.Close(); 
        LoadConf(); 
       } 
       catch (Exception ex) 
       { 
        MessageBox.Show(ex.Message); 
       } 
      } 
      else 
       MessageBox.Show("ERR_F0: Pfad nicht gefunden."); 
     } 
     else 
      MessageBox.Show("ERR_D0: Pfad nicht gefunden.");*/ 
    } 

    private void OBJ_ReloadButton_Click(object sender, RoutedEventArgs e) 
    { 

    } 

    private void OBJ_DataGrid_SelectionChanged(object sender, SelectionChangedEventArgs e) 
    { 

    } 

    private void OBJ_DataGrid_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e) 
    { 
     MessageBox.Show(e.Row.ToString()); 
    } 

    void OnChecked(object sender, RoutedEventArgs e) 
    { 
     MessageBox.Show(e.Source.ToString()); 
    } 


} 

public class ItemGrid 
{ 
    public ItemGrid(string name, bool rval) 
    { 
     ItemName = name; 
     ItemValue = rval; 
    } 

    public string ItemName { set; get; } 
    public bool ItemValue { set; get; } 
} 

public class ItemsGrid : List<ItemGrid> 
{ 
    public string path = null; 
    public string filepath = null; 
    public ItemsGrid() 
    { 
     path = String.Format(@"{0}\build\", Environment.CurrentDirectory); 
     filepath = Path.Combine(path + "configuration.csv"); 
     if (Directory.Exists(path)) 
     { 
      if (File.Exists(filepath)) 
      { 
       string line = null; 
       StreamReader file = new StreamReader(filepath); 
       while ((line = file.ReadLine()) != null) 
       { 
        if (!line.Equals("SFObject;Skip") && !line.Equals("SuccessMSG;")) 
        { 
         string input = (line.IndexOf(";X") != -1 ? (line.Replace(";X", "")) : (line.Replace(";", ""))); 
         Add(new ItemGrid(input, (line.IndexOf(";X") != -1 ? (true) : (false)))); 
        } 
       } 
       file.Close(); 
      } 
      else 
       MessageBox.Show("ERR_F0: Pfad nicht gefunden."); 
     } 
     else 
      MessageBox.Show("ERR_D0: Pfad nicht gefunden."); 


     //Add(new ItemGrid("Tom", false)); 
     // Add(new ItemGrid("Jen", false)); 

    } 

} 

这是它的外观(和应该的样子)。 Image1

CSV:

Image2

我希望你们能帮助我,我真的不明白为什么它不工作。我也不得不承认,我没有精通C#的能力。

回答

0

我假设您粘贴的CSV格式被认为是正确的。

// HelperClass.cs 
using System; 
using System.Collections.Generic; 
using System.IO; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 

namespace WpfCsvSkipTicker 
{ 
    public static class HelperClass 
    { 
     public static List<ItemGrid> ReadCsv(string filepath) 
     { 
      if (!File.Exists(filepath)) return null; 
      var allLines = File.ReadAllLines(filepath); 
      var result = 
       from line in allLines.Skip(1).Take(allLines.Length -2) 
       let temparry = line.Split(';') 
       let isSkip = 
        temparry.Length > 1 
        && temparry[1] != null 
        && temparry[1] == "X" 
       select new ItemGrid { ItemName = temparry[0], ItemValue = !isSkip }; 
      return result.ToList(); 
     } 

     public static void WriteCsv(IEnumerable<ItemGrid> items, string filepath) 
     { 
      var temparray = items.Select(item => item.ItemName + ";" + (item.ItemValue ? "" : "X")).ToArray(); 
      var contents = new string[temparray.Length + 2]; 
      Array.Copy(temparray, 0, contents, 1, temparray.Length); 
      contents[0] = "SFOBject;Skip"; 
      contents[contents.Length - 1] = "SuccessMSG;"; 
      File.WriteAllLines(filepath, contents); 
     } 
    } 

    public class ItemGrid 
    { 
     public string ItemName { set; get; } 
     public bool ItemValue { set; get; } 
    } 
} 

而且......

// MainWindow.xaml.cs 
/// <summary> 
/// Interaction logic for MainWindow.xaml 
/// </summary> 
public partial class MainWindow : Window 
{ 
    public MainWindow() 
    { 
     InitializeComponent(); 
     this.dataGridView.ItemsSource = HelperClass.ReadCsv(@"PathToRead\configuration.csv"); 
    } 

    private void btnSave_Click(object sender, RoutedEventArgs e) 
    { 
     var temp = new List<ItemGrid>(); 
     for (int i = 0; i < this.dataGridView.Items.Count; i++) 
     { 
      if (this.dataGridView.Items[i] is ItemGrid) // DataGrid pads it's item collection with elements we didn't add. 
       temp.Add((ItemGrid)this.dataGridView.Items[i]); 
     } 
     HelperClass.WriteCsv(temp, @"PathToSave\new_configuration.csv"); 
    } 
} 
+1

@WithMetta你好,非常感谢你为这个!现在一切正常。在两个小时内获得第一个Demo,现在就完成了应用程序!谢谢谢谢谢谢! – Pierre

+0

祝你好运! – WithMetta