OleDb を使用してコンマ区切りファイルをロードすると、DataView にバインドできます。
注: 大規模なデータセットはテストしていません。
DataGrid へのバインディングは次のとおりです。
<Window x:Class="DatagridBackgroundWorker.Views.MainView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:WpfToolkit="clr-namespace:Microsoft.Windows.Controls;assembly=WPFToolkit"
Loaded="Window_Loaded"
Title="Main Window" Height="400" Width="800">
<DockPanel>
<Grid>
<WpfToolkit:DataGrid
ItemsSource="{Binding Path=GridData, Mode=OneWay}" >
</WpfToolkit:DataGrid>
</Grid>
</DockPanel>
</Window>
ビューモデルは次のとおりです。
public class MainViewModel : ViewModelBase
{
public MainViewModel()
{
// name of the file
string fileName = "MyData.txt";
// location of the file
string filePath = Environment.CurrentDirectory;
string connection = @"Provider=Microsoft.Jet.OleDb.4.0; Data Source = " +
filePath +
";Extended Properties=\"Text;HDR=Yes;FMT=Delimited\"";
OleDbConnection conn = new OleDbConnection(connection);
conn.Open();
OleDbDataAdapter adapter = new OleDbDataAdapter("SELECT * FROM [" + fileName + "]", conn);
adapter.Fill(_ds);
}
private DataSet _ds = new DataSet("MyDataSet");
public DataView GridData
{
get
{
return _ds.Tables[0].DefaultView;
}
}
}
使用したデータファイルは次のとおりです。
name,site,extra
jeff,codinghorror,stackoverflow
joel,joelonsoftware,stackoverflow
zamboni,secondbeach,stackoverflow