另一个WPF外部.csv绑定困境

时间:2012-02-25 18:43:41

标签: c# wpfdatagrid

我在DataGrid中显示我的数据时遇到问题。 我在网上搜索过,找到了一个可能的解决方案,或者看起来似乎但仍然没有运气。 我想知道你是否可以告诉我代码有什么问题? FIY:我正在将.csv加载到DataTable中,然后将其导出到DataGrid。

这是我的代码:

// DataLoader is a class that loads the data from .csv data file. I've tested it and it works for sure.


// MainWindow.xampl.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Data;
using System.IO;


namespace Splash
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        DataLoader dataTable = new DataLoader();

        public MainWindow()
        {
            InitializeComponent();
        }      

        private void Data_Table(object sender, SelectionChangedEventArgs e)
        {
            string path = @"C:\Users\Lyukshins\Dropbox\PROGRAM_TEST\AUTOMATION\DATA\Database.csv";
            FileInfo theFile = new FileInfo(path);
            dataTable = new DataLoader();
            DataTable table = dataTable.GetDataTableFromCsv(theFile);
            dataGrid.ItemsSource = table.DefaultView;
            dataGrid.AutoGenerateColumns = true;

        }

        private void Copy_Files_Click(object sender, RoutedEventArgs e)
        {

        }

        private void Rename_Click(object sender, RoutedEventArgs e)
        {

        }
    }
}


// XAML 
<Window x:Class="Splash.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Splash" mc:Ignorable="d" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" d:DesignHeight="386" d:DesignWidth="1000" SizeToContent="WidthAndHeight" Name="Splash" ResizeMode="CanResizeWithGrip" Background="#FFBFDEBF">
    <Grid>
        <DataGrid Height="335" HorizontalAlignment="Left" Name="dataGrid" VerticalAlignment="Top" Width="475" SelectionChanged="Data_Table"/>
        <Button Content="Copy Files" Height="54" HorizontalAlignment="Left" Margin="504,12,0,0" Name="Copy_Files" VerticalAlignment="Top" Width="301" Click="Copy_Files_Click" Foreground="Black" FontStyle="Normal" Background="LightSteelBlue" />
        <Button Content="Rename Files" Height="54" HorizontalAlignment="Left" Margin="833,12,0,0" Name="Rename" VerticalAlignment="Top" Width="114" Click="Rename_Click" Background="LightSteelBlue" />
        <Button Content="Distribute Files From Vendors" Height="235" HorizontalAlignment="Left" Margin="504,81,0,0" Name="Distribute_Files_From_Vendors" VerticalAlignment="Top" Width="301" Background="LightSteelBlue" />
        <Button Content=" Create Project &#x0a; Folders" Height="235" HorizontalAlignment="Left" Margin="833,81,0,0" Name="Create_Apollo_File_Structure" VerticalAlignment="Top" Width="114" HorizontalContentAlignment="Center" Background="LightSteelBlue" AllowDrop="False"></Button>
    </Grid>
</Window>

似乎通过设置

解决了问题
dataGrid.ItemsSource = table.DefaultView;
dataGrid.AutoGenerateColumns = true;

但它在我的情况下不起作用。 你能帮忙吗?

1 个答案:

答案 0 :(得分:0)

我能够在别处找到问题的解决方案。 基本的想法是我没有编写显示功能。 通过简单地添加函数private void viewGrid(DataTable table),它解决了我的问题。 这是更新的代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Data;
using System.IO;


namespace Splash
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        DataLoader dataTable = new DataLoader();

        public MainWindow()
        {
            string path = @"C:\Users\Lyukshins\Dropbox\PROGRAM_TEST\AUTOMATION\DATA\Database.csv";
            FileInfo theFile = new FileInfo(path);
            dataTable = new DataLoader();
            DataTable table = dataTable.GetDataTableFromCsv(theFile);

            InitializeComponent();
            viewGrid(table);

        }

        private void viewGrid(DataTable table)
        {        
            if (table.Columns.Count == 0)
                MessageBox.Show("Error!");
            else
                dataGrid.ItemsSource = table.DefaultView;
        }


        private void Data_Table(object sender, SelectionChangedEventArgs e)
        {
            string path = @"C:\Users\Lyukshins\Dropbox\PROGRAM_TEST\AUTOMATION\DATA\Database.csv";
            FileInfo theFile = new FileInfo(path);
            dataTable = new DataLoader();
            DataTable table = dataTable.GetDataTableFromCsv(theFile);                        
        }

        private void Copy_Files_Click(object sender, RoutedEventArgs e)
        {

        }

        private void Rename_Click(object sender, RoutedEventArgs e)
        {

        }
    }
}