2014-11-04 58 views
-1

我想按如下所示在datagrid中顯示數據:

FILE_NAME | FILE_EXTENSION | FILE_PATH
__________ | _______________ | ___________
____ AFile__ | _____。dll ________ | _C:\ ProgramFiles \ Windows \ FileA.dll
____ BFile__ | _____。dll ________ | _C:\ ProgramFiles \ Windows \ FileB.dll
_____ + ____ | _______ + _______ | __________ + ____
_____ + ____ | _______ + _______ | __________ + ____
_____ + ____ | _______ + _______ | __________ + ____
........等等在.csv文件如何導入wpftoolkit中的.csv數據Datagrid

數據存儲爲

ROW1(å文件,.dll文件,C:\ PROGRAMFILES \的Windows \ AFile.exe)

行2(BFILE,.dll文件,C:\ PROGRAMFILES \的Windows \ AFile.exe)
我不知道如何顯示在數據網格以上述方式中的數據,請幫助我。

C#

using System; 
using System.Collections.Generic; 
using System.Data; 
using System.IO; 
using System.Runtime.InteropServices; 
using System.Text; 
using System.Windows; 
using System.Windows.Controls.Primitives; 
using System.Windows.Documents; 
using System.Windows.Controls; 

namespace FileFinder 
{ 
    /// <summary> 
    /// Interaction logic for Window1.xaml 
    /// </summary> 
    public partial class Window1 : Window 
    { 
     public Window1() 
     { 
      InitializeComponent(); 
      myGridView.Visibility = System.Windows.Visibility.Collapsed; 
     } 

     private void getEXEbutton_Click(object sender, RoutedEventArgs e) 
     { 
      List<string> exeResult = new List<string>(); 

      StringBuilder sb1 = new StringBuilder(); 
      StringBuilder sb2 = new StringBuilder(); 
      StringBuilder sb3 = new StringBuilder(); 

      foreach (String fetchList in Directory.GetFiles(@"C:\Program Files (x86)\Panasonic-MES", "*.exe", SearchOption.AllDirectories)) 
      {  
       exeResult.Add(Path.GetFileNameWithoutExtension(fetchList) + "," + Path.GetExtension(fetchList)+"," + Path.GetFullPath(fetchList)); 
       sb1.AppendLine(fetchList); 
       sb2.AppendLine(Path.GetFileNameWithoutExtension(fetchList)); 
       sb3.AppendLine(Path.GetExtension(fetchList)); 
      } 

      File.WriteAllLines(@"C:\BigB.csv", exeResult.ToArray()); 

      textbox_Path.Text = sb1.ToString(); 
      textbox_Filename.Text = sb2.ToString(); 
      textbox_Extension.Text = sb3.ToString(); 
     } 

     private void getDLLbutton_Click(object Sender, RoutedEventArgs e) 
     { 
      List<string> lstResult = new List<string>(); 

      StringBuilder sb1 = new StringBuilder(); 
      StringBuilder sb2 = new StringBuilder(); 
      StringBuilder sb3 = new StringBuilder(); 

      foreach (String fetchList in Directory.GetFiles(@"C:\Program Files (x86)\Panasonic-MES", "*.dll", SearchOption.AllDirectories)) 
      {  
       lstResult.Add(Path.GetFileNameWithoutExtension(fetchList) + "," + Path.GetExtension(fetchList)+"," + Path.GetFullPath(fetchList)); 
       sb1.AppendLine(fetchList); 
       sb2.AppendLine(Path.GetFileNameWithoutExtension(fetchList)); 
       sb3.AppendLine(Path.GetExtension(fetchList)); 
      } 

      File.WriteAllLines(@"C:\BigB.csv", lstResult.ToArray()); 

      textbox_Path.Text = sb1.ToString(); 
      textbox_Filename.Text = sb2.ToString(); 
      textbox_Extension.Text = sb3.ToString(); 
     } 

     private void getALLbutton_Click(object sender, RoutedEventArgs e) 
     { 
      List<string> lstResult = new List<string>(); 

      StringBuilder sb1 = new StringBuilder(); 
      StringBuilder sb2 = new StringBuilder(); 
      StringBuilder sb3 = new StringBuilder(); 

      foreach (String fetchList in Directory.GetFiles(@"C:\Program Files (x86)\Panasonic-MES", "*.exe", SearchOption.AllDirectories)) 
      {  
       lstResult.Add(Path.GetFileNameWithoutExtension(fetchList) + "," + Path.GetExtension(fetchList)+"," + Path.GetFullPath(fetchList)); 
       sb1.AppendLine(fetchList); 
       sb2.AppendLine(Path.GetFileNameWithoutExtension(fetchList)); 
       sb3.AppendLine(Path.GetExtension(fetchList)); 
      } 
      foreach (String fetchList in Directory.GetFiles(@"C:\Program Files (x86)\Panasonic-MES", "*.dll", SearchOption.AllDirectories)) 
      { 
       lstResult.Add(Path.GetFileNameWithoutExtension(fetchList) + "," + Path.GetExtension(fetchList) + "," + Path.GetFullPath(fetchList)); 
       sb1.AppendLine(fetchList); 
       sb2.AppendLine(Path.GetFileNameWithoutExtension(fetchList)); 
       sb3.AppendLine(Path.GetExtension(fetchList)); 
      } 
      File.WriteAllLines(@"C:\BigB.csv", lstResult.ToArray()); 

      textbox_Path.Text = sb1.ToString(); 
      textbox_Filename.Text = sb2.ToString(); 
      textbox_Extension.Text = sb3.ToString(); 
     } 

     private void buttonPageUp_Click(object sender, RoutedEventArgs e) 
     { 
      textbox_Filename.PageUp(); 
      textbox_Extension.PageUp(); 
      textbox_Path.PageUp(); 
     } 

     private void buttonPageDown_Click(object sender, RoutedEventArgs e) 
     { 
      textbox_Filename.PageDown(); 
      textbox_Extension.PageDown(); 
      textbox_Path.PageDown(); 
     } 

     private void SyncScroll(object sender, ScrollChangedEventArgs e) 
     { 
      var textToSync = (sender == textbox_Path) ? textbox_Filename : textbox_Path; 
      textToSync.ScrollToVerticalOffset(e.VerticalOffset); 

      var Synctext = (sender == textbox_Path) ? textbox_Extension : textbox_Path; 
      Synctext.ScrollToVerticalOffset(e.VerticalOffset); 
     } 

     private void buttonGetFile_Click(object sender, RoutedEventArgs e) 
     { 
      textbox_Path.Visibility = System.Windows.Visibility.Collapsed; 
      textbox_Filename.Visibility = System.Windows.Visibility.Collapsed; 
      textbox_Extension.Visibility = System.Windows.Visibility.Collapsed; 
      myGridView.Visibility = System.Windows.Visibility.Visible; 
      SHOWCSV(); 
    } 

     private void SHOWCSV() 
     { 
      // What stuff should I do here to make use of myGridView to show data saved in .csv file as shown above 

      string delimiter = ","; 
      string tableName = "PathTable"; 
      string filePath = @"C:\BigB.csv"; 
      DataSet dataset = new DataSet(); 
      StreamReader sr = new StreamReader(filePath); 
      dataset.Tables.Add(tableName); 
      dataset.Tables[tableName].Columns.Add("File"); 
      dataset.Tables[tableName].Columns.Add("Extension"); 
      dataset.Tables[tableName].Columns.Add("Path"); 

      string allData = sr.ReadToEnd(); 

      string[] rows = allData.Split("\r".ToCharArray()); 

      foreach (string r in rows) 
      { 
       string[] items = r.Split(delimiter.ToCharArray()); 
       dataset.Tables[tableName].Rows.Add(items); 
      } 
     } 
} 

XAML

<Window x:Class="FileFinder.Window1" 
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:wpfx="http://schemas.microsoft.com/wpf/2008/toolkit" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
Title="FileCopier" 
    WindowStartupLocation="CenterScreen" WindowState="Maximized" Width="1350" Height="760"> 
<Grid Background="LavenderBlush"> 

    <Label Height="40" Margin="240,0,234,0" Name="label1" VerticalAlignment="Top" FontSize="28" HorizontalContentAlignment="Center" Foreground="DarkSeaGreen" FontWeight="Bold" FontFamily="Broadway">Welcome To</Label> 

    <Label Height="55" Margin="194,35,194,0" Name="label2" VerticalAlignment="Top" HorizontalContentAlignment="Center" FontWeight="Bold" FontSize="45" Foreground="Chocolate" FontFamily="Colonna MT">FILE FINDER</Label> 



    <Button ToolTip="Click to search exe files" Height="45" HorizontalAlignment="Left" Margin="366,96,0,0" Name="getEXEbutton" VerticalAlignment="Top" Width="112" Background="LavenderBlush" BorderThickness="1" BorderBrush="DarkSeaGreen" FontSize="24" FontWeight="Bold" Foreground="DarkSeaGreen" Click="getEXEbutton_Click" FontFamily="Broadway" Cursor="Hand">.exe</Button> 
    <Button ToolTip="Click to search dll files" Margin="602,96,0,0" Name="getDLLbutton" Background="LavenderBlush" BorderBrush="DarkSeaGreen" Foreground="DarkSeaGreen" FontSize="24" FontWeight="Bold" Click="getDLLbutton_Click" FontFamily="Broadway" Cursor="Hand" Height="45" VerticalAlignment="Top" HorizontalAlignment="Left" Width="123">.dll</Button> 
    <Button Background="LavenderBlush" BorderBrush="DarkSeaGreen" FontSize="24" FontWeight="Bold" Foreground="DarkSeaGreen" Height="45" HorizontalAlignment="Right" Margin="0,96,383,0" Name="getALLbutton" VerticalAlignment="Top" Width="122" Click="getALLbutton_Click" FontFamily="Broadway">All</Button> 
    <Button Background="LavenderBlush" BorderBrush="DarkSeaGreen" FontFamily="Broadway" FontSize="24" FontWeight="Bold" Foreground="DarkSeaGreen" Height="45" HorizontalAlignment="Right" Margin="0,95.743,143,0" Name="GetFilebutton" VerticalAlignment="Top" Width="122" Click="buttonGetFile_Click">GetFile</Button> 

    <Label FontFamily="Broadway" FontSize="28" FontWeight="Bold" Foreground="Black" Height="40" Margin="12,165,0,0" Name="labelFilename" VerticalAlignment="Top" HorizontalAlignment="Left" Width="230.069" Opacity="0.6">Filename</Label> 
    <Label FontFamily="Broadway" FontSize="28" FontWeight="Bold" Foreground="Black" Height="40" Margin="0,165,234,0" Name="labelPath" VerticalAlignment="Top" Opacity="0.6" HorizontalAlignment="Right" Width="86.627">Path</Label> 
    <Label FontFamily="Broadway" FontSize="28" FontWeight="Bold" Foreground="Black" Height="40" HorizontalAlignment="Left" HorizontalContentAlignment="Center" Margin="305.806,165.764,0,0" Name="labelExtension" Opacity="0.6" VerticalAlignment="Top" Width="230.069">Extension</Label> 

    <!--PageUp and PageDown --> 
    <Button Name="buttonPageUp" Click="buttonPageUp_Click" Width="32" Height="32" HorizontalAlignment="Right" Margin="0,173,175,0" VerticalAlignment="Top" BorderThickness="0" Background="LavenderBlush" ToolTip="PageUp"> 
     <StackPanel> 
      <Image Source="C:\Users\Vipul.Sharma\Documents\Visual Studio 2008\Projects\FileFinder\FileFinder\PageUp.png"></Image> 
     </StackPanel> 
    </Button> 

    <Button Name="buttonPageDown" Click="buttonPageDown_Click" Width="32" Height="32" HorizontalAlignment="Right" Margin="0,173,140,0" VerticalAlignment="Top" BorderThickness="0" Background="LavenderBlush" ToolTip="PageUp"> 
     <StackPanel> 
      <Image Source="C:\Users\Vipul.Sharma\Documents\Visual Studio 2008\Projects\FileFinder\FileFinder\PageDown.png" Opacity="0.7"></Image> 
     </StackPanel> 
    </Button> 

    <TextBox x:Name="textbox_Filename" IsReadOnly="True" AutoWordSelection="True" VerticalScrollBarVisibility="Auto" Margin="20,211,0,21" Background="LavenderBlush" Opacity="0.7" ScrollViewer.ScrollChanged="SyncScroll" BorderThickness="0" FontSize="16" TextWrapping="Wrap" HorizontalAlignment="Left" Width="346"></TextBox> 
    <TextBox x:Name="textbox_Extension" IsReadOnly="True" AutoWordSelection="True" VerticalScrollBarVisibility="Auto" Margin="371.741,211.11,0,35.007" Background="LavenderBlush" Opacity="0.7" ScrollViewer.ScrollChanged="SyncScroll" BorderThickness="0" FontSize="16" TextWrapping="Wrap" HorizontalAlignment="Left" Width="106.259"></TextBox> 
    <TextBox x:Name="textbox_Path" IsReadOnly="True" AutoWordSelection="True" VerticalScrollBarVisibility="Auto" Margin="475.857,211,20,21" Background="LavenderBlush" Opacity="0.7" ScrollViewer.ScrollChanged="SyncScroll" BorderThickness="0" FontSize="16" TextWrapping="Wrap"></TextBox> 

    <wpfx:DataGrid Margin="12,165,20,50" Name="myGridView" AlternatingRowBackground="DarkSeaGreen" Background="LightPink" Panel.ZIndex="-1"> 
     <wpfx:DataGrid.Columns> 
      <wpfx:DataGridTextColumn x:Name="filename" Header="FileName" Binding="{}"></wpfx:DataGridTextColumn> 
      <wpfx:DataGridTextColumn x:Name="extension" Header="Extension" Binding="{}"></wpfx:DataGridTextColumn> 
      <wpfx:DataGridTextColumn x:Name="path" Header="FilePath" Binding="{}"></wpfx:DataGridTextColumn> 
     </wpfx:DataGrid.Columns> 
    </wpfx:DataGrid> 
</Grid> 

+0

您有沒有具體的問題?你試過什麼了? – stijn 2014-11-04 11:43:23

回答

1

做的,以下列

1)請閱讀使用某種方法CSV文件的數據(FileStream或其他東西)
2)使用字符串分割函數分離CSV(您可以找到許多方法來讀取CSV文件)
3)解析並格式化每個字符串輸出並創建一個類型列表List<File>(Se下面的類結構)並填寫列表中的項目
4)將List綁定到ItemSource屬性中的DataGrid。

public class File 
{ 
    public String FileName 
    { 
     get; 
     set; 
    } 

    public string Filepath 
    { 
     get; 
     set; 
    } 

} 

嘗試谷歌搜索如果不清楚你任何一步......大多數文獻的幫你做這已經是可用的,也構成下一次,後而提出問題否則你的問題,你已經嘗試了什麼將被降低。

+0

謝謝先生,關於您要遵循的方法的真棒建議。作爲一個初學者,起初看起來很複雜,但是你的逐步描述幫助了很多。 – 2014-11-05 11:10:12