root/Baza Reklam 2 - Faktury/OrderViewer.cs @ 65

Wersja 65, 10.3 KB (wprowadzona przez dorota, 17 years temu)
Line 
1using System;
2using System.Collections.Generic;
3using System.ComponentModel;
4using System.Data;
5using System.Data.SqlClient;
6using System.Drawing;
7using System.Text;
8using System.Windows.Forms;
9using Microsoft.Reporting.WinForms;
10using System.Drawing.Printing;
11using System.Drawing.Imaging;
12using System.IO;
13using System.Reflection;
14
15namespace Baza_Reklam
16{
17    public partial class OrderViewer : Form
18    {
19        private int reklamaId;
20        DataSet ds;
21
22        public OrderViewer(int idRek)
23        {
24            InitializeComponent();
25            reklamaId = idRek;
26                     
27            ds = new DataSet();
28
29            zamowienieTableAdapter.Connection.ConnectionString = ConnString.getConnString().Value;
30
31            Stream s = Assembly.GetExecutingAssembly().GetManifestResourceStream("Baza_Reklam.Raporty.zamowienie.xml");
32            ds.ReadXml(s);
33            reportViewer1.LocalReport.DataSources.Add(new ReportDataSource("tlumaczenia", ds.Tables[0]));
34            reportViewer1.LocalReport.DataSources.Add(new ReportDataSource("zamowienie", zamowienieTableAdapter.GetDataByReklamaId(reklamaId)));
35
36           
37            SqlCommand cm = new SqlCommand();
38            cm.Connection = new SqlConnection();
39            cm.CommandText = "declare @test varchar(800); set @test = ''; declare @nr_wyd int; " +
40                " DECLARE kur2 CURSOR FOR SELECT [Nr Wydania] FROM [UKA¯E SIÊ W NR] WHERE (ReklamaId = @param) order by [Nr Wydania];" +
41                " OPEN kur2; FETCH NEXT from kur2 into @nr_wyd; while @@fetch_status=0 begin   set @test  = @test + ', ' + CAST(@nr_wyd AS varchar); " +
42                " fetch next from kur2 into @nr_wyd; end CLOSE kur2; DEALLOCATE kur2; select @test; ";
43            cm.Parameters.AddWithValue("@param", reklamaId);
44            cm.Connection.ConnectionString = ConnString.getConnString().Value;
45
46            string emisje;
47            cm.Connection.Open();
48            emisje = (string)cm.ExecuteScalar();
49            cm.Connection.Close();
50
51            raporty r = new raporty();
52
53            raporty.emisjeRow row = r.emisje.NewemisjeRow();
54            row.ID = emisje;
55            r.emisje.AddemisjeRow(row);
56
57            reportViewer1.LocalReport.DataSources.Add(new ReportDataSource("raporty_emisje", r.emisje));
58                       
59            //this.reportViewer1.Print += new CancelEventHandler(HandlePrint);
60
61            jezykComboBox.SelectedIndex = 0;
62
63        }
64
65        public OrderViewer(int idZam, int a)
66        {
67            InitializeComponent();
68            reklamaId = idZam;
69
70            ds = new DataSet();
71
72            zamowienie2TableAdapter.Connection.ConnectionString = ConnString.getConnString().Value;
73
74            reportViewer1.LocalReport.ReportEmbeddedResource = "Baza_Reklam.Raporty.zamowienie2.rdlc";
75       
76            Stream s = Assembly.GetExecutingAssembly().GetManifestResourceStream("Baza_Reklam.Raporty.zamowienie.xml");
77            ds.ReadXml(s);
78            reportViewer1.LocalReport.DataSources.Add(new ReportDataSource("tlumaczenia", ds.Tables[0]));
79            raporty.Zamowienie2DataTable t= zamowienie2TableAdapter.GetDataByIdZamowienia(idZam);
80            reportViewer1.LocalReport.DataSources.Add(new ReportDataSource("zamowienie2", t));
81            //this.reportViewer1.Print += new CancelEventHandler(HandlePrint);
82
83           
84            jezykComboBox.SelectedIndex = 0;
85
86        }
87
88        private void OrderViewer_Load(object sender, EventArgs e)
89        {
90            this.reportViewer1.RefreshReport();
91        }
92
93        // drukowanie
94       
95        /*
96        private int m_currentPageIndex;
97        private IList<Stream> m_streams;
98
99
100        public void HandlePrint(object sender, CancelEventArgs e)
101        {
102           Run();
103           //MessageBox.Show("Drukowanie");
104           e.Cancel = true;
105        }
106
107        private void Print(PrinterSettings printerSettings)
108        {
109            if (m_streams == null || m_streams.Count == 0)
110                return;
111
112            PrintDocument printDoc = new PrintDocument();
113            printDoc.PrinterSettings = printerSettings;
114            printDoc.PrintPage += new PrintPageEventHandler(PrintPage);
115            printDoc.Print();
116        }
117
118        private void Run()
119        {
120            PrintDialog printDialog = new PrintDialog();
121            if (printDialog.ShowDialog() == DialogResult.OK)
122            {
123                Export(reportViewer1.ServerReport);
124                m_currentPageIndex = 0;
125                Print(printDialog.PrinterSettings);
126            }
127        }
128
129        private void PrintPage(object sender, PrintPageEventArgs ev)
130        {
131            Metafile pageImage = new Metafile(m_streams[m_currentPageIndex]);
132           
133            // Note: Coordinate (0,0) does not coincide with the top left corner of
134            // the page; it coincides with the top left corner of the printable area
135            // of the page. To account for this we have to subtract the hard margin.
136
137            RectangleF adjustedRect = new RectangleF(
138                ev.PageBounds.Left - ev.PageSettings.HardMarginX,
139                ev.PageBounds.Top - ev.PageSettings.HardMarginY,
140                ev.PageBounds.Width, ev.PageBounds.Height);
141           
142            ev.Graphics.DrawImage(pageImage, adjustedRect);
143            m_currentPageIndex++;
144            ev.HasMorePages = (m_currentPageIndex < m_streams.Count);
145
146        }
147
148        private void Export(ServerReport report)
149        {
150            string deviceInfo =
151
152              "<DeviceInfo>" +
153
154              "  <OutputFormat>EMF</OutputFormat>" +
155
156              "  <PageWidth>21cm</PageWidth>" +
157
158              "  <PageHeight>29cm</PageHeight>" +
159
160              "  <MarginTop>1cm</MarginTop>" +
161
162              "  <MarginLeft>1.5cm</MarginLeft>" +
163
164              "  <MarginRight>1.5cm</MarginRight>" +
165
166              "  <MarginBottom>1cm</MarginBottom>" +
167
168              "</DeviceInfo>";
169           
170            m_streams = new List<Stream>();
171            string mimeType;
172            string extension;
173            System.Collections.Specialized.NameValueCollection urlAccessParameters = new System.Collections.Specialized.NameValueCollection();
174            urlAccessParameters.Add("rs:PersistStreams", "True");
175            Stream reportStream = report.Render("Image", deviceInfo, urlAccessParameters, out mimeType, out extension);
176            m_streams.Add(reportStream);
177            urlAccessParameters.Remove("rs:PersistStreams");
178            urlAccessParameters.Add("rs:GetNextStream", "True");
179            while (reportStream.Length != 0)
180            {
181                reportStream =
182                    report.Render("Image", deviceInfo, urlAccessParameters, out mimeType, out extension);
183                m_streams.Add(reportStream);
184            }
185
186            m_streams.RemoveAt(m_streams.Count - 1);
187
188            foreach (Stream stream in m_streams)
189                stream.Position = 0;
190        }
191        */
192        private void zapiszButton_Click(object sender, EventArgs e)
193        {
194            this.Cursor = Cursors.WaitCursor;
195
196            List<ReportParameter> paramList = new List<ReportParameter>();
197            paramList.Add(new ReportParameter("jezyk", jezykComboBox.SelectedItem.ToString(), true));
198            this.reportViewer1.LocalReport.SetParameters(paramList);
199            reportViewer1.RefreshReport();
200
201            this.Cursor = Cursors.Default;
202
203        }
204
205        // drukowanie
206        private int m_currentPageIndex;
207        private IList<Stream> m_streams;
208
209        private Stream CreateStream(string name,
210          string fileNameExtension, Encoding encoding,
211          string mimeType, bool willSeek)
212        {
213            Stream stream = new FileStream(name +
214               "." + fileNameExtension, FileMode.Create);
215            m_streams.Add(stream);
216            return stream;
217        }
218
219        private void Export(LocalReport report)
220        {
221            string deviceInfo =
222              "<DeviceInfo>" +
223              "  <OutputFormat>EMF</OutputFormat>" +
224              "  <PageWidth>21cm</PageWidth>" +
225              "  <PageHeight>29.7cm</PageHeight>" +
226              "  <MarginTop>1cm</MarginTop>" +
227              "  <MarginLeft>1.4cm</MarginLeft>" +
228              "  <MarginRight>1.4cm</MarginRight>" +
229              "  <MarginBottom>0cm</MarginBottom>" +
230              "</DeviceInfo>";
231            Warning[] warnings;
232            m_streams = new List<Stream>();
233            report.Render("Image", deviceInfo, CreateStream,
234               out warnings);
235            foreach (Stream stream in m_streams)
236                stream.Position = 0;
237        }
238
239        private void PrintPage(object sender, PrintPageEventArgs ev)
240        {
241            Metafile pageImage = new
242               Metafile(m_streams[m_currentPageIndex]);
243            ev.Graphics.DrawImage(pageImage, ev.PageBounds);           
244            m_currentPageIndex++;
245            ev.HasMorePages = (m_currentPageIndex < m_streams.Count);
246        }
247
248        private void Print(PrinterSettings printerSettings)
249        {
250            if (m_streams == null || m_streams.Count == 0)
251                return;
252
253            PrintDocument printDoc = new PrintDocument();
254            printDoc.PrinterSettings = printerSettings;
255            printDoc.PrinterSettings.Duplex = Duplex.Vertical;
256            printDoc.PrintPage += new PrintPageEventHandler(PrintPage);
257            printDoc.Print();
258        }
259
260        private void Run()
261        {
262             PrintDialog printDialog = new PrintDialog();
263             if (printDialog.ShowDialog() == DialogResult.OK)
264             {
265                 Export(reportViewer1.LocalReport);
266                 m_currentPageIndex = 0;
267                 Print(printDialog.PrinterSettings);
268             }
269           
270        }
271
272        public void Dispose()
273        {
274            if (m_streams != null)
275            {
276                foreach (Stream stream in m_streams)
277                    stream.Close();
278                m_streams = null;
279            }
280            Dispose();
281        }
282
283        public void HandlePrint(object sender, CancelEventArgs e)
284        {
285            Run();
286            e.Cancel = true;
287        }
288
289    }
290}
Notatka: Zobacz TracBrowser aby uzyskać więcej informacji.