-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHistory.cs
159 lines (140 loc) · 6.26 KB
/
History.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
using System;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Windows.Forms;
namespace Leaernify
{
public partial class History : UserControl
{
public History()
{
InitializeComponent();
getData();
}
SqlConnection con = new SqlConnection(@"Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=Learnify;Integrated Security=True;");
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
if (dataGridView1.Columns[e.ColumnIndex].Name == "Aksi" && e.RowIndex >= 0)
{
var statusCell = dataGridView1.Rows[e.RowIndex].Cells["Status"].Value;
if (statusCell != null && statusCell.ToString() != "Booked")
{
MessageBox.Show("Hanya booking dengan status 'Booked' yang bisa dibatalkan!", "Peringatan", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
var bookingIDCell = dataGridView1.Rows[e.RowIndex].Cells["Booking_ID"].Value;
if (bookingIDCell != null)
{
string bookingID = bookingIDCell.ToString();
DialogResult result = MessageBox.Show("Apakah Anda yakin ingin membatalkan booking?",
"Konfirmasi", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (result == DialogResult.Yes)
{
try
{
con.Open();
string queryBooking = "UPDATE Booking SET status = 'Pending cancel' WHERE id = @bookingID";
using (SqlCommand cmdBooking = new SqlCommand(queryBooking, con))
{
cmdBooking.Parameters.AddWithValue("@bookingID", bookingID);
cmdBooking.ExecuteNonQuery();
}
MessageBox.Show("Booking berhasil dibatalkan!", "Sukses", MessageBoxButtons.OK, MessageBoxIcon.Information);
getData();
}
catch (Exception ex)
{
MessageBox.Show("Terjadi kesalahan: " + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
finally
{
con.Close();
}
}
}
else
{
MessageBox.Show("Data Booking ID tidak ditemukan!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
}
public void getData()
{
try
{
con.Open();
int userID = userSession.id;
string query = @"
SELECT
b.id AS Booking_ID,
b.pengajar_id AS pengajar_id,
p.nama AS Nama_Pengajar,
FORMAT(b.tanggal_booking, 'dd MMM yyyy - HH:mm') AS Tanggal_Booking,
b.status AS Status
FROM Booking b
JOIN Pengajar p ON b.pengajar_id = p.id
WHERE b.user_id = @id AND b.status != 'cancelled'";
SqlDataAdapter sda = new SqlDataAdapter(query, con);
sda.SelectCommand.Parameters.AddWithValue("@id", userID);
DataTable dt = new DataTable();
sda.Fill(dt);
dataGridView1.DataSource = dt;
dataGridView1.Columns["Booking_ID"].HeaderText = "ID Booking";
dataGridView1.Columns["Nama_Pengajar"].HeaderText = "Nama Pengajar";
dataGridView1.Columns["Tanggal_Booking"].HeaderText = "Tanggal Booking";
dataGridView1.Columns["Status"].HeaderText = "Status Booking";
if (dataGridView1.Columns["Aksi"] == null)
{
DataGridViewButtonColumn btnCancel = new DataGridViewButtonColumn();
btnCancel.HeaderText = "Aksi";
btnCancel.Text = "Cancel Booking";
btnCancel.UseColumnTextForButtonValue = true;
btnCancel.Name = "Aksi";
dataGridView1.Columns.Add(btnCancel);
}
foreach (DataGridViewRow row in dataGridView1.Rows)
{
var statusCell = row.Cells["Status"].Value;
if (statusCell != null)
{
string status = statusCell.ToString();
if (status == "Booked")
{
row.Cells["Status"].Style.ForeColor = Color.Blue;
}
else if (status == "Cancelled")
{
row.Cells["Status"].Style.ForeColor = Color.Red;
}
}
}
}
catch (Exception ex)
{
MessageBox.Show("Terjadi kesalahan: " + ex.Message);
}
finally
{
con.Close();
}
}
private void History_Load(object sender, EventArgs e)
{
dataGridView1.CellClick += dataGridView1_CellContentClick;
}
private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
if (dataGridView1.Columns[e.ColumnIndex].Name == "Aksi" && e.RowIndex >= 0)
{
var statusCell = dataGridView1.Rows[e.RowIndex].Cells["Status"].Value;
if (statusCell != null && statusCell.ToString() != "Booked")
{
dataGridView1.Rows[e.RowIndex].Cells["Aksi"].ReadOnly = true;
dataGridView1.Rows[e.RowIndex].Cells["Aksi"].Style.ForeColor = Color.Gray;
dataGridView1.Rows[e.RowIndex].Cells["Aksi"].Style.BackColor = Color.LightGray;
}
}
}
}
}