У меня проблемы с отображением моих данных в 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 
 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;
, но в моем случае это не сработало.Не могли бы вы помочь?