如何在列表上打印文档,即DGV C#的数据源
我的应用程序有一个 DataGridView,其中列出了所有选定的文件并将其添加到作为 DGV 数据源的类中。
public partial class Form1 : Form
{
BindingList<Datei> dateienList = new BindingList<Datei>();
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = "Word(*.docx)| *.docx|PPT(*.pptx)|*.pptx|PDF(*.pdf)|*.pdf|Alle Dateien(*.*)|*.*";//filter is an attribute
ofd.Multiselect = true;
if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK) //the next code executes only if ok is clicked
{
foreach (String path in ofd.FileNames)
{
Datei datei = new Datei();
datei.filePath = path;
datei.Dateiname = Path.GetFileName(path);
dateienList.Add(datei);
}
}
}
}
数据源类:
public class Datei
{
[DisplayName("Dateiname")]
public string Dateiname { get; set; }
[DisplayName("Neue Dateiname")]
public string NeueDateiname { get; set; }
public string filePath { get; set; }
public string newFilePath { get; set; }
}
我想制作一个按钮来打印列表中的所有文档。这就是我尝试过的:
private void button2_Click(object sender, EventArgs e)
{
PrintDialog printDialog1 = new PrintDialog();
PrintDocument printDocument = new PrintDocument();
printDialog1.AllowSelection = true;
printDialog1.AllowSomePages = true;
printDialog1.AllowCurrentPage = true;
if (printDialog1.ShowDialog() == DialogResult.OK)
{
foreach (Datei datei in dateienList)
{
printDocument.Print(); // guess it is totally wrong...
}
}
}
它没有按预期工作,打印出空白。任何帮助表示赞赏。
my app has a DataGridView where all selected files get listed and added to a class which is the datasource of the DGV.
public partial class Form1 : Form
{
BindingList<Datei> dateienList = new BindingList<Datei>();
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = "Word(*.docx)| *.docx|PPT(*.pptx)|*.pptx|PDF(*.pdf)|*.pdf|Alle Dateien(*.*)|*.*";//filter is an attribute
ofd.Multiselect = true;
if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK) //the next code executes only if ok is clicked
{
foreach (String path in ofd.FileNames)
{
Datei datei = new Datei();
datei.filePath = path;
datei.Dateiname = Path.GetFileName(path);
dateienList.Add(datei);
}
}
}
}
datasource class:
public class Datei
{
[DisplayName("Dateiname")]
public string Dateiname { get; set; }
[DisplayName("Neue Dateiname")]
public string NeueDateiname { get; set; }
public string filePath { get; set; }
public string newFilePath { get; set; }
}
I want to make a button to print out all the documents on the list. This is what I tried:
private void button2_Click(object sender, EventArgs e)
{
PrintDialog printDialog1 = new PrintDialog();
PrintDocument printDocument = new PrintDocument();
printDialog1.AllowSelection = true;
printDialog1.AllowSomePages = true;
printDialog1.AllowCurrentPage = true;
if (printDialog1.ShowDialog() == DialogResult.OK)
{
foreach (Datei datei in dateienList)
{
printDocument.Print(); // guess it is totally wrong...
}
}
}
It did not work as expected, printing out blank. Any help is appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

我无法避免打开Word程序,但至少它可以工作。
I couldn't avoid opening Word program, but at least it works.