-
Notifications
You must be signed in to change notification settings - Fork 52
Expand file tree
/
Copy pathMemoryContent.CopyExport.cs
More file actions
149 lines (133 loc) · 5.99 KB
/
MemoryContent.CopyExport.cs
File metadata and controls
149 lines (133 loc) · 5.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/*
* Copyright (c) 2026 Erik Darling, Darling Data LLC
*
* This file is part of the SQL Server Performance Monitor.
*
* Licensed under the MIT License. See LICENSE file in the project root for full license information.
*/
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Data;
using Microsoft.Win32;
using PerformanceMonitorDashboard.Helpers;
using PerformanceMonitorDashboard.Models;
using PerformanceMonitorDashboard.Services;
namespace PerformanceMonitorDashboard.Controls
{
public partial class MemoryContent : UserControl
{
#region Context Menu Handlers
private void CopyCell_Click(object sender, RoutedEventArgs e)
{
if (sender is MenuItem menuItem && menuItem.Parent is ContextMenu contextMenu)
{
var dataGrid = TabHelpers.FindDataGridFromContextMenu(contextMenu);
if (dataGrid != null && dataGrid.CurrentCell.Item != null)
{
var cellContent = TabHelpers.GetCellContent(dataGrid, dataGrid.CurrentCell);
if (!string.IsNullOrEmpty(cellContent))
{
/* Use SetDataObject with copy=false to avoid WPF's problematic Clipboard.Flush() */
Clipboard.SetDataObject(cellContent, false);
}
}
}
}
private void CopyRow_Click(object sender, RoutedEventArgs e)
{
if (sender is MenuItem menuItem && menuItem.Parent is ContextMenu contextMenu)
{
var dataGrid = TabHelpers.FindDataGridFromContextMenu(contextMenu);
if (dataGrid != null && dataGrid.SelectedItem != null)
{
var rowText = TabHelpers.GetRowAsText(dataGrid, dataGrid.SelectedItem);
/* Use SetDataObject with copy=false to avoid WPF's problematic Clipboard.Flush() */
Clipboard.SetDataObject(rowText, false);
}
}
}
private void CopyAllRows_Click(object sender, RoutedEventArgs e)
{
if (sender is MenuItem menuItem && menuItem.Parent is ContextMenu contextMenu)
{
var dataGrid = TabHelpers.FindDataGridFromContextMenu(contextMenu);
if (dataGrid != null && dataGrid.Items.Count > 0)
{
var sb = new StringBuilder();
// Add headers
var headers = new List<string>();
foreach (var column in dataGrid.Columns)
{
if (column is DataGridBoundColumn)
{
headers.Add(Helpers.DataGridClipboardBehavior.GetHeaderText(column));
}
}
sb.AppendLine(string.Join("\t", headers));
// Add all rows
foreach (var item in dataGrid.Items)
{
sb.AppendLine(TabHelpers.GetRowAsText(dataGrid, item));
}
/* Use SetDataObject with copy=false to avoid WPF's problematic Clipboard.Flush() */
Clipboard.SetDataObject(sb.ToString(), false);
}
}
}
private void ExportToCsv_Click(object sender, RoutedEventArgs e)
{
if (sender is MenuItem menuItem && menuItem.Parent is ContextMenu contextMenu)
{
var dataGrid = TabHelpers.FindDataGridFromContextMenu(contextMenu);
if (dataGrid != null && dataGrid.Items.Count > 0)
{
string prefix = "memory";
var saveFileDialog = new SaveFileDialog
{
FileName = $"{prefix}_{DateTime.Now:yyyyMMdd_HHmmss}.csv",
DefaultExt = ".csv",
Filter = "CSV Files (*.csv)|*.csv|All Files (*.*)|*.*"
};
if (saveFileDialog.ShowDialog() == true)
{
try
{
var sb = new StringBuilder();
// Add headers
var headers = new List<string>();
foreach (var column in dataGrid.Columns)
{
if (column is DataGridBoundColumn)
{
headers.Add(TabHelpers.EscapeCsvField(Helpers.DataGridClipboardBehavior.GetHeaderText(column), TabHelpers.CsvSeparator));
}
}
sb.AppendLine(string.Join(TabHelpers.CsvSeparator, headers));
// Add all rows
foreach (var item in dataGrid.Items)
{
var values = TabHelpers.GetRowValues(dataGrid, item);
sb.AppendLine(string.Join(TabHelpers.CsvSeparator, values.Select(v => TabHelpers.EscapeCsvField(v, TabHelpers.CsvSeparator))));
}
File.WriteAllText(saveFileDialog.FileName, sb.ToString());
MessageBox.Show($"Data exported successfully to:\n{saveFileDialog.FileName}", "Export Complete", MessageBoxButton.OK, MessageBoxImage.Information);
}
catch (Exception ex)
{
MessageBox.Show($"Error exporting data:\n\n{ex.Message}", "Export Error", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
}
}
}
#endregion
}
}