-1

I want to show the data in the datagrid as follows :

FILE_NAME | FILE_EXTENSION | FILE_PATH
__________| _______________|___________
____AFile__| _____.dll________|_C:\ProgramFiles\Windows\FileA.dll
____BFile__| _____.dll________|_C:\ProgramFiles\Windows\FileB.dll
_____+____| _______+_______|__________+____
_____+____| _______+_______|__________+____
_____+____| _______+_______|__________+____
........so on

Data in .csv file is stored as

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

Row2 (BFile , .dll , C:\ProgramFiles\Windows\AFile.exe)
I have no idea how to show the data in above manner in datagrid, please help me out.

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 Answers1

1

Do the Following in order

1) Read data of the CSV File using Some Method (FileStream or something other)
2) Seperate CSV using string split functions (You can find numerous methods to read a csv file)
3) Parse and Format each string output and Create a list of type List<File> (Se below for class structure) and fill items in the list
4) Bind the List to a DataGrid in the ItemSource property.

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

   public string Filepath
   {
       get;
       set;
   }  

}  

Try googling if any step in unclear to you... most of the literature to help you do this is already available, Also form next time, post what you have tried while asking questions otherwise your question will be downvoted.

Ankesh
  • 4,847
  • 4
  • 38
  • 76
  • Thank you sir for your awesome suggestion regarding the approach to be followed. Being a beginner,it seemed complex task at first but your step-by-step description helped a lot. – Vipul Sharma Nov 05 '14 at 11:10