带有复选框的Wpf gridview:在按钮单击时获取选定的报告文件行 [英] Wpf gridview with checkbox: getting selected rows of report files on button click
问题描述
我使用WPF创建了一个应用程序,其中一个按钮(选择)将文件夹浏览器打开到文本框中提供的默认/指定路径中的文件夹,并且数据网格中填充了该文件夹中的所有文件(.rpt文件) 。数据网格中的列是路径,文件名,还有一个复选框列。我想使用复选框检索我在数据网格中选择的文件,这应该通过单击按钮来完成,因为按钮将发送这些文件到了打印服务。我提出的代码我设法在消息框中显示所选文件的路径但我仍然坚持如何从网格中检索这些文件并通过单击此按钮将它们发送到打印服务。所以主要是我有两个问题:
1.复选框没有显示已经检查的所有行,只显示了第一个/最后一个选定的行
2.我想提取文件的文件名/路径,以便将文件发送到打印服务
我尝试过:
命名空间DataGridCheckBoxDisplayApp
{
public partial class MainWindow:Window
{
public class ReportFile
{
public string Path {get;组; }
公共字符串FileName {get;组; }
}
private void Button_Click(object sender,RoutedEventArgs e)
{
string inputPath = AppDomain.CurrentDomain.BaseDirectory;
System.Windows.Forms.FolderBrowserDialog fldDlg = new System.Windows.Forms.FolderBrowserDialog();
fldDlg.SelectedPath = AppDomain.CurrentDomain.BaseDirectory;
DialogResult result = fldDlg.ShowDialog();
{
foreach(Directory.GetFiles中的字符串str(fldDlg.SelectedPath))
{
ReportFile reportFile = new ReportFile();
reportFile.Path = str;
reportFile.FileName = System.IO.Path.GetFileName(str);
dataGrid1.Items.Add(reportFile);
}
}
}
private void button_Click_1(对象发送者,RoutedEventArgs e)
{
foreach(ReportFile drv in dataGrid1.SelectedItems.OfType< ReportFile>())
{
if(drv!= null)
{
string path = drv。路径;
//drv.Path = drv.ItemArray [3] .ToString();
System.Windows.MessageBox.Show(drv.Path);
}
}
var TransactionFactory = new TransactionFactory();
var Transaction = TransactionFactory.NewTransactionString();
var EnvironmentValue =(string)cmbEnvironment.SelectedValue;
var CirieEngineServiceClientFactory = new CirieEngineServiceClientFactory(EnvironmentValue);
var CirieEngineServiceClient = CirieEngineServiceClientFactory.NewCirieEngineServiceClient();
var Form = new Cirie.Form()
{
Path = string.Empty,
Title = string.Empty
};
var PackageID = Convert.ToInt16(txtPackageID.SelectedText);
var Generation = Convert.ToInt16(txtGeneration.SelectedText);
var formList = new List< Cirie.Form>();
var stream = CirieEngineServiceClient.PrintFormCollection
(交易,
新集合< Cirie.Form>(formList),
PackageID,
代
);
}
}
}
如果你在<$ c中添加一个布尔字段$ c> ReportFile 类模型名为IsChecked
或IsSelected
并将复选框绑定到它,那么你只需要检查每个模型。例如:var selected = dataGrid1.Items.Where(x = > x.IsChecked)。ToList();不要忘记
使用 System.Linq;
I created an application using WPF in which a button(select) opens the folder browser to a folder in a default/specified path provided in a textbox and the datagrid is populated with all the files in that folder(.rpt files). The columns in the datagrid are path, filename and there is also a column for checkbox.I want to retrieve the files that I select in the datagrid using the checkbox and this should be done by clicking on a button as the button will send these files to a printing service.The code that I came up with I managed to display the path of the selected file in a message box but I am stuck on how to retreive these files from the grid and send them to the printing service with this button click. So mainly I have two problems:
1. The checkbox is not displaying all the rows which have been checked, only the first/ last selected row is being displayed
2. I want to extract the filename/path of the file whichever works to get the files to be sent to the printing service
What I have tried:
namespace DataGridCheckBoxDisplayApp { public partial class MainWindow : Window { public class ReportFile { public string Path { get; set; } public string FileName { get; set; } } private void Button_Click(object sender, RoutedEventArgs e) { string inputPath = AppDomain.CurrentDomain.BaseDirectory; System.Windows.Forms.FolderBrowserDialog fldDlg = new System.Windows.Forms.FolderBrowserDialog(); fldDlg.SelectedPath = AppDomain.CurrentDomain.BaseDirectory; DialogResult result = fldDlg.ShowDialog(); { foreach (string str in Directory.GetFiles(fldDlg.SelectedPath)) { ReportFile reportFile = new ReportFile(); reportFile.Path = str; reportFile.FileName = System.IO.Path.GetFileName(str); dataGrid1.Items.Add(reportFile); } } } private void button_Click_1(object sender, RoutedEventArgs e) { foreach (ReportFile drv in dataGrid1.SelectedItems.OfType<ReportFile>()) { if (drv != null) { string path = drv.Path; //drv.Path = drv.ItemArray[3].ToString(); System.Windows.MessageBox.Show(drv.Path); } } var TransactionFactory = new TransactionFactory(); var Transaction = TransactionFactory.NewTransactionString(); var EnvironmentValue = (string)cmbEnvironment.SelectedValue; var CirieEngineServiceClientFactory = new CirieEngineServiceClientFactory(EnvironmentValue); var CirieEngineServiceClient = CirieEngineServiceClientFactory.NewCirieEngineServiceClient(); var Form = new Cirie.Form() { Path = string.Empty, Title = string.Empty }; var PackageID = Convert.ToInt16(txtPackageID.SelectedText); var Generation = Convert.ToInt16(txtGeneration.SelectedText); var formList = new List<Cirie.Form>(); var stream = CirieEngineServiceClient.PrintFormCollection (Transaction, new Collection<Cirie.Form>(formList), PackageID, Generation ); } } }
If you add a boolean field to yourReportFile
class model calledIsChecked
orIsSelected
and bind the checkbox to it, then you simply need to check each model. Eg:var selected = dataGrid1.Items.Where(x => x.IsChecked).ToList();Don't forget
using System.Linq;
这篇关于带有复选框的Wpf gridview:在按钮单击时获取选定的报告文件行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!