/
FormFC.cs
151 lines (119 loc) · 4.44 KB
/
FormFC.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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Threading;
using System.Data.SqlClient;
namespace QBisnis
{
public partial class FormFC : Form
{
static string connectionString = @"Data Source=LAPTOP-CKRPB9BI\MSSQLSERVER1;Initial Catalog=QBisnis;Integrated Security=True";
static SqlConnection connection = new SqlConnection(connectionString);
int id = 0;
public FormFC()
{
InitializeComponent();
Clear();
Sow();
TotalSemua();
}
void Clear()
{
tbNominal.Text = tbKategori.Text = tbNote.Text = "";
}
public double TotalSemua()
{
double total = 0;
tbTotalFC.Text = "0";
for (int i = 0; i < DataGridView1.Rows.Count; i++)
{
total += Convert.ToDouble(DataGridView1.Rows[i].Cells[2].Value);
}
tbTotalFC.Text = total.ToString();
return total;
}
private void Sow()
{
connection.Open();
SqlCommand command = connection.CreateCommand();
command = new SqlCommand("Select * from FixedCost", connection);
DataTable dt = new DataTable();
SqlDataReader sdr = command.ExecuteReader();
dt.Load(sdr);
connection.Close();
DataGridView1.DataSource = dt;
}
private void btnAdd_Click(object sender, EventArgs e)
{
if (tbNominal.Text != "" && tbKategori.Text != "" && tbNote.Text != "")
{
connection.Open();
SqlCommand cmd = new SqlCommand("insert into FixedCost values (@Kategori,@Nominal,@Note)",connection);
cmd.Parameters.AddWithValue("@Kategori", tbKategori.Text);
cmd.Parameters.AddWithValue("@Nominal", double.Parse(tbNominal.Text));
cmd.Parameters.AddWithValue("@Note", tbNote.Text);
int i = cmd.ExecuteNonQuery();
if (i > 0)
{
MessageBox.Show("Record Successfully Saved");
}
else
{
MessageBox.Show("Record Not Saved");
}
connection.Close();
Sow();
TotalSemua();
}
}
private void DataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
id = Convert.ToInt32(DataGridView1.SelectedRows[0].Cells[0].Value);
tbNominal.Text = DataGridView1.SelectedRows[0].Cells[1].Value.ToString();
tbKategori.Text = DataGridView1.SelectedRows[0].Cells[2].Value.ToString();
tbNote.Text = DataGridView1.SelectedRows[0].Cells[3].Value.ToString();
}
private void btnHapus_Click(object sender, EventArgs e)
{
foreach (DataGridViewRow item in this.DataGridView1.SelectedRows)
{
//string connectionString = @"Data Source=LAPTOP-CKRPB9BI\MSSQLSERVER1;Initial Catalog=QBisnis;Integrated Security=True";
using (SqlConnection connection = new SqlConnection(connectionString))
{
SqlCommand cmd = connection.CreateCommand();
int id = Convert.ToInt32(DataGridView1.SelectedRows[0].Cells[0].Value);
cmd.CommandText = "Delete from FixedCost where id='" + id + "'";
DataGridView1.Rows.RemoveAt(this.DataGridView1.SelectedRows[0].Index);
connection.Open();
cmd.ExecuteNonQuery();
}
}
TotalSemua();
}
private void FormFC_Load(object sender, EventArgs e)
{
Clear();
Sow();
TotalSemua();
}
public double totalfc()
{
double total;
Sow();
total = TotalSemua();
return total;
}
private void btnHome_Click(object sender, EventArgs e)
{
FormUtama form = new FormUtama(tbTotalFC.Text);
form.Show();
this.Hide();
}
}
}