如何在wpftoolkit Datagrid中导入.csv数据

时间:2014-11-04 11:37:54

标签: .net wpf datagrid wpftoolkit

我想在datagrid中显示数据如下:

FILE_NAME | FILE_EXTENSION | FILE_PATH
__________ | _______________ | ___________
____ AFile__ | _____ DLL ________ | _C:\ PROGRAMFILES \的Windows \ FileA.dll
____ BFile__ | _____ DLL ________ | _C:\ PROGRAMFILES \的Windows \ FileB.dll
_____ + ____ | _______ + _______ | __________ + ____
_____ + ____ | _______ + _______ | __________ + ____
_____ + ____ | _______ + _______ | __________ + ____
........等等

.csv文件中的数据存储为

  

Row1(AFile,.dll,C:\ ProgramFiles \ Windows \ AFile.exe)

     

Row2(BFile,.dll,C:\ ProgramFiles \ Windows \ AFile.exe)   
  我不知道如何在datagrid中以上述方式显示数据,请帮帮我。

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>

1 个答案:

答案 0 :(得分:1)

按顺序执行以下操作

1)使用某种方法(FileStream或其他)读取CSV文件的数据 2)使用字符串拆分功能分离CSV(您可以找到许多方法来读取csv文件)
3)解析并格式化每个字符串输出并创建类型List<File>的列表(类结构下面的Se)并填写列表中的项目
4)将List绑定到ItemSource属性中的DataGrid。

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

   public string Filepath
   {
       get;
       set;
   }  

}  

如果您不清楚任何步骤,请尝试使用Google搜索...大部分帮助您执行此操作的文献已经可用,下次还会形成发布您在提问时尝试的内容否则您的问题将被低估。