-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfrmProductsRecord.cs
222 lines (204 loc) · 11.4 KB
/
frmProductsRecord.cs
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using Excel = Microsoft.Office.Interop.Excel;
using System.IO;
namespace Sales_and_Inventory_System__Gadgets_Shop_
{
public partial class frmProductsRecord : Form
{
SqlDataReader rdr = null;
SqlConnection con = null;
SqlCommand cmd = null;
ConnectionString cs = new ConnectionString();
public frmProductsRecord()
{
InitializeComponent();
}
public void GetData()
{
try
{
con = new SqlConnection(cs.DBConn);
con.Open();
cmd = new SqlCommand("SELECT RTRIM(ProductID) as[ProductID], RTRIM(ProductName) as [Product Name],RTRIM(BrandName) as [Brand Name],RTRIM(CompanyName) as [Company Name],RTRIM(Category.ID) as [Category Id],RTRIM(CategoryName) as [Category Name],RTRIM(SubCategory.ID) as [SubCategory Id],RTRIM(SubCategoryName) as [SubCategory Name],RTRIM(Price) as [Retail Price],RTRIM(RetailPrice) as [Purchased Price],RTRIM(Features) as [Discription],RTRIM(CompanyAdress) as [Company Adress],RTRIM(Type) as [Type] from Product,Category,SubCategory where Product.CategoryID=Category.ID and Product.SubCategoryID=SubCategory.ID order by Productname", con);
SqlDataAdapter myDA = new SqlDataAdapter(cmd);
DataSet myDataSet = new DataSet();
myDA.Fill(myDataSet, "Product");
myDA.Fill(myDataSet, "Category");
myDA.Fill(myDataSet, "SubCategory");
dataGridView1.DataSource = myDataSet.Tables["SubCategory"].DefaultView;
dataGridView1.DataSource = myDataSet.Tables["Category"].DefaultView;
dataGridView1.DataSource = myDataSet.Tables["Product"].DefaultView;
con.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void frmProductsRecord_Load(object sender, EventArgs e)
{
GetData();
}
private void Button3_Click(object sender, EventArgs e)
{
int rowsTotal = 0;
int colsTotal = 0;
int I = 0;
int j = 0;
int iC = 0;
System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.WaitCursor;
Excel.Application xlApp = new Excel.Application();
try
{
Excel.Workbook excelBook = xlApp.Workbooks.Add();
Excel.Worksheet excelWorksheet = (Excel.Worksheet)excelBook.Worksheets[1];
xlApp.Visible = true;
rowsTotal = dataGridView1.RowCount - 1;
colsTotal = dataGridView1.Columns.Count - 1;
var _with1 = excelWorksheet;
_with1.Cells.Select();
_with1.Cells.Delete();
for (iC = 0; iC <= colsTotal; iC++)
{
_with1.Cells[1, iC + 1].Value = dataGridView1.Columns[iC].HeaderText;
}
for (I = 0; I <= rowsTotal - 1; I++)
{
for (j = 0; j <= colsTotal; j++)
{
_with1.Cells[I + 2, j + 1].value = dataGridView1.Rows[I].Cells[j].Value;
}
}
_with1.Rows["1:1"].Font.FontStyle = "Bold";
_with1.Rows["1:1"].Font.Size = 12;
_with1.Cells.Columns.AutoFit();
_with1.Cells.Select();
_with1.Cells.EntireColumn.AutoFit();
_with1.Cells[1, 1].Select();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
finally
{
//RELEASE ALLOACTED RESOURCES
System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.Default;
xlApp = null;
}
}
private void frmProductsRecord_FormClosing(object sender, FormClosingEventArgs e)
{
}
private void dataGridView1_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
{
string strRowNumber = (e.RowIndex + 1).ToString();
SizeF size = e.Graphics.MeasureString(strRowNumber, this.Font);
if (dataGridView1.RowHeadersWidth < Convert.ToInt32((size.Width + 20)))
{
dataGridView1.RowHeadersWidth = Convert.ToInt32((size.Width + 20));
}
Brush b = SystemBrushes.ControlText;
e.Graphics.DrawString(strRowNumber, this.Font, b, e.RowBounds.Location.X + 15, e.RowBounds.Location.Y + ((e.RowBounds.Height - size.Height) / 2));
}
private void txtProductname_TextChanged(object sender, EventArgs e)
{
try
{
con = new SqlConnection(cs.DBConn);
con.Open();
cmd = new SqlCommand("SELECT RTRIM(ProductID) as[ProductID], RTRIM(ProductName) as [Product Name],RTRIM(BrandName) as [Brand Name],RTRIM(CompanyName) as [Company Name],RTRIM(Category.ID) as [Category Id],RTRIM(CategoryName) as [Category Name],RTRIM(SubCategory.ID) as [SubCategory Id],RTRIM(SubCategoryName) as [SubCategory Name],RTRIM(Price) as [Retail Price],RTRIM(RetailPrice) as [Purchased Price],RTRIM(Features) as [Discription],RTRIM(CompanyAdress) as [Company Adress],RTRIM(Type) as [Type] from Product,Category,SubCategory where Product.CategoryID=Category.ID and Product.SubCategoryID=SubCategory.ID and ProductName like '" + txtProductname.Text + "%' order by Productname", con);
SqlDataAdapter myDA = new SqlDataAdapter(cmd);
DataSet myDataSet = new DataSet();
myDA.Fill(myDataSet, "Product");
myDA.Fill(myDataSet, "Category");
myDA.Fill(myDataSet, "SubCategory");
dataGridView1.DataSource = myDataSet.Tables["SubCategory"].DefaultView;
dataGridView1.DataSource = myDataSet.Tables["Category"].DefaultView;
dataGridView1.DataSource = myDataSet.Tables["Product"].DefaultView;
con.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void txtCategory_TextChanged(object sender, EventArgs e)
{
try
{
con = new SqlConnection(cs.DBConn);
con.Open();
cmd = new SqlCommand("SELECT RTRIM(ProductID) as[ProductID], RTRIM(ProductName) as [Product Name],RTRIM(BrandName) as [Brand Name],RTRIM(CompanyName) as [Company Name],RTRIM(Category.ID) as [Category Id],RTRIM(CategoryName) as [Category Name],RTRIM(SubCategory.ID) as [SubCategory Id],RTRIM(SubCategoryName) as [SubCategory Name],RTRIM(Price) as [Retail Price],RTRIM(RetailPrice) as [Purchased Price],RTRIM(Features) as [Discription],RTRIM(CompanyAdress) as [Company Adress],RTRIM(Type) as [Type] from Product,Category,SubCategory where Product.CategoryID=Category.ID and Product.SubCategoryID=SubCategory.ID and CategoryName like '" + txtCategory.Text + "%' order by Productname", con);
SqlDataAdapter myDA = new SqlDataAdapter(cmd);
DataSet myDataSet = new DataSet();
myDA.Fill(myDataSet, "Product");
myDA.Fill(myDataSet, "Category");
myDA.Fill(myDataSet, "SubCategory");
dataGridView1.DataSource = myDataSet.Tables["SubCategory"].DefaultView;
dataGridView1.DataSource = myDataSet.Tables["Category"].DefaultView;
dataGridView1.DataSource = myDataSet.Tables["Product"].DefaultView;
con.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void txtSubCategory_TextChanged(object sender, EventArgs e)
{
try
{
con = new SqlConnection(cs.DBConn);
con.Open();
cmd = new SqlCommand("SELECT RTRIM(ProductID) as[ProductID], RTRIM(ProductName) as [Product Name],RTRIM(BrandName) as [Brand Name],RTRIM(CompanyName) as [Company Name],RTRIM(Category.ID) as [Category Id],RTRIM(CategoryName) as [Category Name],RTRIM(SubCategory.ID) as [SubCategory Id],RTRIM(SubCategoryName) as [SubCategory Name],RTRIM(Price) as [Retail Price],RTRIM(RetailPrice) as [Purchased Price],RTRIM(Features) as [Discription],RTRIM(CompanyAdress) as [Company Adress],RTRIM(Type) as [Type] from Product,Category,SubCategory where Product.CategoryID=Category.ID and Product.SubCategoryID=SubCategory.ID and SubCategoryName like '" + txtSubCategory.Text + "%' order by Productname", con);
SqlDataAdapter myDA = new SqlDataAdapter(cmd);
DataSet myDataSet = new DataSet();
myDA.Fill(myDataSet, "Product");
myDA.Fill(myDataSet, "Category");
myDA.Fill(myDataSet, "SubCategory");
dataGridView1.DataSource = myDataSet.Tables["SubCategory"].DefaultView;
dataGridView1.DataSource = myDataSet.Tables["Category"].DefaultView;
dataGridView1.DataSource = myDataSet.Tables["Product"].DefaultView;
con.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
try
{
con = new SqlConnection(cs.DBConn);
con.Open();
cmd = new SqlCommand("SELECT RTRIM(ProductID) as[ProductID], RTRIM(ProductName) as [Product Name],RTRIM(BrandName) as [Brand Name],RTRIM(CompanyName) as [Company Name],RTRIM(Category.ID) as [Category Id],RTRIM(CategoryName) as [Category Name],RTRIM(SubCategory.ID) as [SubCategory Id],RTRIM(SubCategoryName) as [SubCategory Name],RTRIM(Price) as [Retail Price],RTRIM(RetailPrice) as [Purchased Price],RTRIM(Features) as [Discription],RTRIM(CompanyAdress) as [Company Adress],RTRIM(Type) as [Type] from Product,Category,SubCategory where Product.CategoryID=Category.ID and Product.SubCategoryID=SubCategory.ID and BrandName like '" + BrandName.Text + "%' order by Productname", con);
SqlDataAdapter myDA = new SqlDataAdapter(cmd);
DataSet myDataSet = new DataSet();
myDA.Fill(myDataSet, "Product");
myDA.Fill(myDataSet, "Category");
myDA.Fill(myDataSet, "SubCategory");
dataGridView1.DataSource = myDataSet.Tables["SubCategory"].DefaultView;
dataGridView1.DataSource = myDataSet.Tables["Category"].DefaultView;
dataGridView1.DataSource = myDataSet.Tables["Product"].DefaultView;
con.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void groupBox5_Enter(object sender, EventArgs e)
{
}
}
}