Skip to content

Commit 903f558

Browse files
committed
feat: Add export to CSV functionality
1 parent 9d0287a commit 903f558

File tree

3 files changed

+32
-0
lines changed

3 files changed

+32
-0
lines changed

SimpleDataGrid.Example/AdvancedExamplesViewModel.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
using System.Collections.Generic;
44
using System.ComponentModel;
55
using System.Linq;
6+
using System.IO;
7+
using System.Reflection;
68

79
namespace SimpleDataGrid.Example;
810

@@ -79,6 +81,29 @@ public void ApplyCustomFilter()
7981
People.SetFilter("customFilter", p => p.Age > 30);
8082
}
8183

84+
public void ExportCurrentPageToCsv()
85+
{
86+
var items = People.CurrentPageItems;
87+
if (!items.Any()) return;
88+
89+
var csv = new System.Text.StringBuilder();
90+
91+
// Add header row
92+
var headers = typeof(Person).GetProperties().Select(p => p.Name);
93+
csv.AppendLine(string.Join(",", headers));
94+
95+
// Add data rows
96+
foreach (Person item in items)
97+
{
98+
var fields = typeof(Person).GetProperties().Select(p => p.GetValue(item)?.ToString() ?? string.Empty);
99+
csv.AppendLine(string.Join(",", fields));
100+
}
101+
102+
var filePath = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "CurrentPage.csv");
103+
System.IO.File.WriteAllText(filePath, csv.ToString());
104+
System.Windows.MessageBox.Show($"Current page exported to {filePath}");
105+
}
106+
82107
public event PropertyChangedEventHandler? PropertyChanged;
83108
protected virtual void OnPropertyChanged(string propertyName)
84109
{

SimpleDataGrid.Example/AdvancedExamplesWindow.xaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@
9090
<TextBox x:Name="PageTextBox" Width="30" Margin="5,0,0,0"/>
9191
<Button Content="Go" Click="GoToPageButton_Click" Margin="5,0,0,0"/>
9292
<ComboBox Margin="10,0,0,0" ItemsSource="{Binding PageSizes}" SelectedItem="{Binding People.PageSize}"/>
93+
<Button Content="Export Current Page to CSV" Click="ExportToCsvButton_Click" Margin="10,0,0,0"/>
9394
<TextBlock VerticalAlignment="Center" Margin="10,0,0,0">
9495
<TextBlock.Text>
9596
<MultiBinding Converter="{StaticResource ItemCountConverter}">

SimpleDataGrid.Example/AdvancedExamplesWindow.xaml.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,4 +112,10 @@ private void ApplyCustomFilterButton_Click(object sender, RoutedEventArgs e)
112112
var viewModel = (AdvancedExamplesViewModel)DataContext;
113113
viewModel.ApplyCustomFilter();
114114
}
115+
116+
private void ExportToCsvButton_Click(object sender, RoutedEventArgs e)
117+
{
118+
var viewModel = (AdvancedExamplesViewModel)DataContext;
119+
viewModel.ExportCurrentPageToCsv();
120+
}
115121
}

0 commit comments

Comments
 (0)