Hey David,
I just had a similar problem, and I was able to print the contents of a listView by using this example:
http://www.codeproject.com/cs/miscctrl/PrintableListView.asp
The irritating thing with printing listViews is with Cell Wrapping since the List View does not allow multiple lines or text wrapping. To get around this cell wrapping problem, just insert "\n" into the text string in relevant places such as every 40 characters, etc.
Here's some code...
private void buttonPrint_Click(object sender, EventArgs e) { PrintableListView printableListView = new PrintableListView(); printableListView.Columns.Add("Column 1", 100, HorizontalAlignment.Left);
printableListView.Columns.Add("Column 2", 100, HorizontalAlignment.Left);
printableListView.Columns.Add("Column 3", 100, HorizontalAlignment.Left);
} // Wrap results. If multiple entries in Result column, then they are separated by a ";". foreach (ListViewItem lvi in this.listViewAlertHistory.Items) { string lviResults = ""; string[] results = lvi.SubItems[2].Text.Split(';'); if(results.Length > 1) { foreach(string result in results) { lviResults = lviResults + result.Trim() + "\n"; } } else { lviResults = lvi.SubItems[2].Text.Trim(); }
ListViewItem aItem = new ListViewItem(); aItem.SubItems[0].Text = lvi.SubItems[0].Text; aItem.SubItems.Add(lvi.SubItems[1].Text); aItem.SubItems.Add(lviResults); printableListView.Items.Add(aItem); }
printableListView.Title = "Alert History Results"; printableListView.FitToPage = true; printableListView.PrintPreview();
// To print...
// printableListView.Print();
}
|