基本上我正在做的是从本地驱动器收集文件并将它们全部复制到 C:\ReportFiles。我创建了两个按钮。附加并保存。附加将浏览并获取文件。在 dataGrid1 中,我采用旧路径和文件名。收集完所有文件后。点击保存按钮。这将从数据网格中获取路径列,并开始将数据网格路径列中的文件复制到 C:\ReportFiles。循环将完成这项工作。
问题是列表,我不能将数据网格列作为集合列表。希望这能清除。
数据网格代码:
<DataGrid AutoGenerateColumns="False" Height="193" HorizontalAlignment="Left" Margin="169,6,0,0" Name="dataGrid1" VerticalAlignment="Top" Width="200" ItemsSource="{Binding ReportFiles}">
<DataGrid.Columns>
<DataGridTextColumn Header="Path" Binding="{Binding Path}" />
<DataGridTextColumn Header="FileName" Binding="{Binding FileName}" />
</DataGrid.Columns>
</DataGrid>
主窗口.cs:
public partial class EditReport : Window
{
public List<ReportFiles> listabove = new List<ReportFiles>();
public class ReportFiles
{
public string RealName { get; set; }
public string TargetName { get; set; }
}
private void buttonAttach_Click(object sender, RoutedEventArgs e)
{
Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
if (dlg.ShowDialog() == true)
{
foreach (string str in dlg.FileNames)
{
ReportFiles list = new ReportFiles();
list.RealName = str;
list.TargetName = filename;
dataGrid1.Items.Add(new { RealName = list.RealName, TargetName = list.TargetName });
string fileName = @"C:\Temp\" + filename + System.IO.Path.GetExtension(str).Trim(); ;
if (File.Exists(fileName))
continue;
else
{
try
{
File.Copy(str, fileName);
}
catch (Exception err)
{
MessageBox.Show(err.Message);
return;
}
}
}
}
}
private void btnSave_Click(object sender, RoutedEventArgs e)
{
ReportFiles rep = new ReportFiles();
DataRowView paths = (System.Data.DataRowView)dataGrid1.Items[0];
rep.RealName = Convert.ToString(paths.Row.ItemArray[0]);
}
}