001.
using
System;
002.
using
System.Collections.Generic;
003.
using
System.ComponentModel;
004.
using
System.Data;
005.
using
System.Drawing;
006.
using
System.Linq;
007.
using
System.Text;
008.
using
System.Windows.Forms;
009.
using
System.Data.SqlClient;
010.
using
System.Data.Sql;
011.
using
System.Data.SqlTypes;
012.
using
System.IO;
013.
using
System.Drawing.Printing;
014.
using
Microsoft.VisualBasic;
015.
016.
017.
namespace
WinFormSheet28
018.
{
019.
public
partial
class
Form2 : Form
020.
{
021.
public
Form2()
022.
023.
{
024.
InitializeComponent();
025.
}
026.
027.
private
void
Form2_Load(
object
sender, EventArgs e)
028.
{
029.
030.
this
.orderTableAdapter.Fill(
this
.lab2DataSet1order.order);
031.
032.
this
.productTableAdapter.Fill(
this
.lab2DataSetproduct.Product);
033.
034.
DB.connectionString = @
"Data Source=DESKTOP-E3U2G8E;Initial Catalog=lab2;Integrated Security=True"
;
035.
036.
int
[] amount =
new
int
[13];
037.
int
[] num =
new
int
[13];
038.
listView1.Columns.Add(
"เลขที่ใบสั่ง"
, 80, HorizontalAlignment.Center);
039.
listView1.Columns.Add(
"รหัสสินค้า"
, 80, HorizontalAlignment.Center);
040.
listView1.Columns.Add(
"ชื่อสินค้า"
, 100, HorizontalAlignment.Center);
041.
listView1.Columns.Add(
"จำนวน"
, 80, HorizontalAlignment.Center);
042.
listView1.Columns.Add(
"ราคา"
, 80, HorizontalAlignment.Center);
043.
listView1.Columns.Add(
"รวมเป็นเงิน"
, 80, HorizontalAlignment.Center);
044.
listView1.Columns.Add(
"วันที่สั่ง"
, 80, HorizontalAlignment.Center);
045.
listView1.View = View.Details;
046.
listView1.GridLines =
true
;
047.
listView1.FullRowSelect =
true
;
048.
049.
SqlConnection conn =
new
SqlConnection(@
"Data Source=DESKTOP-E3U2G8ES;Initial Catalog=lab2;Integrated Security=True"
);
050.
dateTimepicker1.Enabled =
false
;
051.
052.
}
053.
public
void
show()
054.
{
055.
string
sql =
"select *from order"
;
056.
dataGridView2.DataSource = DB.ExcecuteQuery(sql);
057.
}
058.
059.
private
void
ButtonSave_Click(
object
sender, EventArgs e)
060.
{
061.
062.
SqlConnection conn =
new
SqlConnection(@
"Data Source=DESKTOP-E3U2G8E;Initial Catalog=lab2;Integrated Security=True"
);
063.
064.
for
(
int
j = 0; j <= listView1.Items.Count - 1; j++)
065.
{
066.
string
query = @"INSERT INTO order (order_id,pro_id,pro_name,amount,money,total,dateorder)
067.
VALUES(@order_id,@pro_id,@pro_name,@amount,@money,@total,@dateorder)";
068.
069.
List<SqlParameter> sqlParams =
new
List<SqlParameter>();
070.
sqlParams.Add(
new
SqlParameter(
"@order_id"
, txtID.Text));
071.
sqlParams.Add(
new
SqlParameter(
"@pro_id"
, listView1.Items[j].SubItems[0].Text));
072.
sqlParams.Add(
new
SqlParameter(
"@pro_name"
, listView1.Items[j].SubItems[1].Text));
073.
sqlParams.Add(
new
SqlParameter(
"@amount"
, listView1.Items[j].SubItems[2].Text));
074.
sqlParams.Add(
new
SqlParameter(
"@money"
, listView1.Items[j].SubItems[3].Text));
075.
sqlParams.Add(
new
SqlParameter(
"@total"
, listView1.Items[j].SubItems[4].Text));
076.
sqlParams.Add(
new
SqlParameter(
"@dateorder"
, listView1.Items[j].SubItems[5].Text));
077.
078.
079.
080.
if
(DB.ExecuteNonQuery(query, sqlParams))
081.
{
082.
MessageBox.Show(
"คำสั่งซื้อเสร็จสิ้น"
);
083.
show();
084.
085.
}
086.
else
087.
{
088.
MessageBox.Show(
"เกิดข้อผิดพลาด ไม่สามารถสั่งสินค้าได้"
,
"เกิดข้อผิดพลาด"
, MessageBoxButtons.OK, MessageBoxIcon.Error);
089.
}
090.
}
091.
092.
093.
}
094.
private
void
MaxID()
095.
{
096.
int
maxId = 0;
097.
098.
string
sql = @
"SELECT MAX(order_id) FROM order"
;
099.
dataGridView2.DataSource = DB.ExcecuteQuery(sql);
100.
101.
try
102.
{
103.
104.
105.
}
106.
catch
(Exception ex)
107.
{
108.
maxId = 0;
109.
}
110.
txtID.Text = (maxId + 1).ToString(
"000"
);
111.
textBox1.Text = txtID.Text;
112.
}
113.
114.
115.
private
void
ButtonAdd_Click(
object
sender, EventArgs e)
116.
{
117.
int
i = 0;
118.
ListViewItem lvi;
119.
int
tmpProductID = 0;
120.
string
amountif = dataGridView1.CurrentRow.Cells[2].Value.ToString();
121.
122.
if
(
int
.Parse(txtProQty.Text) <=
int
.Parse(amountif))
123.
{
124.
for
(i = 0; i <= listView1.Items.Count - 1; i++)
125.
{
126.
tmpProductID =
int
.Parse(listView1.Items[i].SubItems[0].Text);
127.
if
(
int
.Parse(txtProID.Text.Trim()) == tmpProductID)
128.
{
129.
MessageBox.Show(
"รหัสสินค้าซ้ำกัน!"
);
130.
txtProID.Focus();
131.
return
;
132.
}
133.
}
134.
}
135.
string
[] anydata;
136.
anydata =
new
string
[]
137.
{ txtID.Text,
138.
txtProID.Text,
139.
txtProName.Text,
140.
txtProQty.Text,
141.
txtProPrice.Text,
142.
txtTotal.Text,
143.
dateTimepicker1.Text
144.
145.
};
146.
lvi =
new
ListViewItem(anydata);
147.
listView1.Items.Add(lvi);
148.
149.
150.
allsumcal();
151.
amountallsumcal();
152.
153.
154.
}
155.
156.
private
void
allsumcal()
157.
{
158.
int
i = 0;
159.
double
tmptotal = 0;
160.
for
(i = 0; i <= listView1.Items.Count - 1; i++)
161.
{
162.
tmptotal +=
double
.Parse(listView1.Items[i].SubItems[4].Text);
163.
}
164.
165.
lblTotal.Text = tmptotal.ToString(
"#,##0.00"
);
166.
}
167.
private
void
amountallsumcal()
168.
{
169.
int
i = 0;
170.
int
tmptotal = 0;
171.
for
(i = 0; i <= listView1.Items.Count - 1; i++)
172.
{
173.
tmptotal +=
int
.Parse(listView1.Items[i].SubItems[3].Text);
174.
}
175.
176.
txt.Text = tmptotal.ToString(
"#,##"
);
177.
}
178.
179.
180.
private
void
EnabledTrue()
181.
{
182.
dateTimepicker1.Enabled =
true
;
183.
txtProID.Enabled =
true
;
184.
txtProQty.Enabled =
true
;
185.
listView1.Enabled =
true
;
186.
187.
MaxID();
188.
189.
}
190.
191.
private
void
EnabledFalse()
192.
{
193.
dateTimepicker1.Enabled =
false
;
194.
txtProID.Enabled =
false
;
195.
txtProQty.Enabled =
false
;
196.
listView1.Enabled =
false
;
197.
198.
MaxID();
199.
}
200.
201.
202.
203.
private
void
calamount()
204.
{
205.
double
total;
206.
total = (
double
.Parse(txtProPrice.Text)) * (
double
.Parse(txtProQty.Text));
207.
txtTotal.Text = total.ToString(
"#,##0.00"
);
208.
209.
210.
}
211.
212.
213.
private
void
txtTotal_TextChanged(
object
sender, EventArgs e)
214.
{
215.
calamount();
216.
217.
}
218.
219.
private
void
listView1_MouseDoubleClick(
object
sender, MouseEventArgs e)
220.
{
221.
int
i = 0;
222.
for
(i = 0; i <= listView1.SelectedItems.Count - 1; i++)
223.
{
224.
ListViewItem lvi;
225.
lvi = listView1.SelectedItems[i];
226.
listView1.Items.Remove(lvi);
227.
}
228.
229.
}
230.
231.
private
void
dataGridView1_CellContentClick_1(
object
sender, DataGridViewCellEventArgs e)
232.
{
233.
txtProID.Text = dataGridView1.CurrentRow.Cells[0].Value.ToString();
234.
txtProName.Text = dataGridView1.CurrentRow.Cells[1].Value.ToString();
235.
txtProPrice.Text = dataGridView1.CurrentRow.Cells[4].Value.ToString();
236.
}
237.
238.
private
void
txtProQty_TextChanged(
object
sender, EventArgs e)
239.
{
240.
calamount();
241.
242.
}
243.
244.
private
void
txt_TextChanged(
object
sender, EventArgs e)
245.
{
246.
amountallsumcal();
247.
}
248.
249.
private
void
ButtonCancel_Click(
object
sender, EventArgs e)
250.
{
251.
Form f10 =
new
main();
252.
f10.Show();
253.
this
.Hide();
254.
255.
}
256.
257.
}
258.
259.
}