-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReportForm.cs
More file actions
148 lines (129 loc) · 4.91 KB
/
ReportForm.cs
File metadata and controls
148 lines (129 loc) · 4.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
using System;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Windows.Forms;
using iTextSharp.text;
using iTextSharp.text.pdf;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;
namespace YourNamespace
{
public partial class ReportForm : Form
{
public ReportForm()
{
InitializeComponent();
comboBox1.Items.AddRange(new string[] { "Contacts", "Messages", "CallLogs", "DeviceInfo", "Accounts" });
}
// Load data from database
private void button1_Click(object sender, EventArgs e)
{
string tableName = "";
switch (comboBox1.SelectedItem.ToString())
{
case "Contacts": tableName = "ContactsTable"; break;
case "Messages": tableName = "MessagesTable"; break;
case "CallLogs": tableName = "CallLogsTable"; break;
case "DeviceInfo": tableName = "DeviceInfoTable"; break;
case "Accounts": tableName = "AccountsTable"; break;
default: MessageBox.Show("Invalid selection."); return;
}
using (SqlConnection conn = new SqlConnection("Your_Connection_String_Here"))
{
SqlDataAdapter adapter = new SqlDataAdapter("SELECT * FROM " + tableName, conn);
DataTable dt = new DataTable();
adapter.Fill(dt);
dataGridView1.DataSource = dt;
}
}
// Export to CSV
private void button2_Click(object sender, EventArgs e)
{
if (dataGridView1.Rows.Count == 0)
{
MessageBox.Show("No data to export.");
return;
}
SaveFileDialog sfd = new SaveFileDialog
{
Filter = "CSV files (*.csv)|*.csv",
FileName = "Report.csv"
};
if (sfd.ShowDialog() == DialogResult.OK)
{
using (StreamWriter sw = new StreamWriter(sfd.FileName))
{
// Header
for (int i = 0; i < dataGridView1.Columns.Count; i++)
{
sw.Write(dataGridView1.Columns[i].HeaderText);
if (i < dataGridView1.Columns.Count - 1)
sw.Write(",");
}
sw.WriteLine();
// Rows
foreach (DataGridViewRow row in dataGridView1.Rows)
{
if (!row.IsNewRow)
{
for (int i = 0; i < row.Cells.Count; i++)
{
sw.Write(row.Cells[i].Value?.ToString());
if (i < row.Cells.Count - 1)
sw.Write(",");
}
sw.WriteLine();
}
}
}
MessageBox.Show("CSV exported successfully.");
}
}
// Export to PDF
private void button3_Click(object sender, EventArgs e)
{
if (dataGridView1.Rows.Count == 0)
{
MessageBox.Show("No data to export.");
return;
}
SaveFileDialog sfd = new SaveFileDialog
{
Filter = "PDF files (*.pdf)|*.pdf",
FileName = "Report.pdf"
};
if (sfd.ShowDialog() == DialogResult.OK)
{
Document doc = new Document();
PdfWriter.GetInstance(doc, new FileStream(sfd.FileName, FileMode.Create));
doc.Open();
PdfPTable pdfTable = new PdfPTable(dataGridView1.Columns.Count);
// Headers
foreach (DataGridViewColumn column in dataGridView1.Columns)
{
pdfTable.AddCell(new Phrase(column.HeaderText));
}
// Rows
foreach (DataGridViewRow row in dataGridView1.Rows)
{
if (!row.IsNewRow)
{
foreach (DataGridViewCell cell in row.Cells)
{
pdfTable.AddCell(new Phrase(cell.Value?.ToString()));
}
}
}
doc.Add(pdfTable);
doc.Close();
MessageBox.Show("PDF exported successfully.");
}
}
<<<<<<< HEAD
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
}
=======
>>>>>>> 01b559577af082ecd5c259b23c559fd26b1d3468
}
}