-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDigital_Content_Modules.cs
180 lines (159 loc) · 6.59 KB
/
Digital_Content_Modules.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
using System;
using System.Data;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace TC_CRM
{
public partial class Digital_Content_Modules : Form
{
private DataGridView dataGridViewModules;
private Button btnBack;
private Button btnBook;
private Label lblModulesTitle;
private Label lblModulesDescription;
private Label lblStatus;
// MySQL Connection String
private string connectionString = "Server=localhost;Database=TC_CRM;Uid=root;Pwd=;";
public Digital_Content_Modules()
{
InitializeComponent();
InitializeUI();
LoadModulesData();
}
// Initialize all UI components programmatically
private void InitializeUI()
{
// Initialize DataGridView
dataGridViewModules = new DataGridView
{
AllowUserToAddRows = false,
AllowUserToDeleteRows = false,
AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill,
Location = new System.Drawing.Point(12, 100),
Name = "dataGridViewModules",
ReadOnly = true,
RowHeadersWidth = 51,
RowTemplate = { Height = 24 },
Size = new System.Drawing.Size(760, 300)
};
// Add "Book" button column to DataGridView
DataGridViewButtonColumn bookButtonColumn = new DataGridViewButtonColumn
{
Name = "BookButton",
Text = "Book",
UseColumnTextForButtonValue = true
};
// Add the "Book" button column to the DataGridView
if (!dataGridViewModules.Columns.Contains("BookButton"))
{
dataGridViewModules.Columns.Add(bookButtonColumn);
}
dataGridViewModules.CellContentClick += dataGridViewModules_CellContentClick;
// Initialize Book button
btnBook = new Button
{
Location = new System.Drawing.Point(12, 420),
Name = "btnBook",
Size = new System.Drawing.Size(75, 23),
Text = "Book Module"
};
btnBook.Click += btnBook_Click;
// Initialize Back button
btnBack = new Button
{
Location = new System.Drawing.Point(100, 420),
Name = "btnBack",
Size = new System.Drawing.Size(75, 23),
Text = "Back"
};
btnBack.Click += btnBack_Click;
// Initialize Labels for Modules Information
lblModulesTitle = new Label
{
Location = new System.Drawing.Point(12, 20),
Size = new System.Drawing.Size(200, 20),
Text = "Available Digital Content Modules"
};
lblModulesDescription = new Label
{
Location = new System.Drawing.Point(12, 50),
Size = new System.Drawing.Size(200, 20),
Text = "Select a module to book"
};
lblStatus = new Label
{
Location = new System.Drawing.Point(12, 70),
Size = new System.Drawing.Size(200, 20),
Text = "Status: All modules are available"
};
// Add controls to the form
Controls.Add(dataGridViewModules);
Controls.Add(btnBook);
Controls.Add(btnBack);
Controls.Add(lblModulesTitle);
Controls.Add(lblModulesDescription);
Controls.Add(lblStatus);
}
// Load and display modules data from the database
private void LoadModulesData()
{
using (MySqlConnection conn = new MySqlConnection(connectionString))
{
MySqlDataAdapter adapter = new MySqlDataAdapter("SELECT * FROM Modules", conn);
DataTable modulesTable = new DataTable();
adapter.Fill(modulesTable);
dataGridViewModules.DataSource = modulesTable;
}
}
// Handle cell click event for the "Book" button
private void dataGridViewModules_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex == dataGridViewModules.Columns["BookButton"].Index && e.RowIndex >= 0)
{
DataGridViewRow row = dataGridViewModules.Rows[e.RowIndex];
string moduleName = row.Cells["ModuleName"].Value.ToString();
bool isAvailable = Convert.ToBoolean(row.Cells["Available"].Value);
if (isAvailable)
{
// Confirm booking and update the module's status in the database
using (MySqlConnection conn = new MySqlConnection(connectionString))
{
MySqlCommand cmd = new MySqlCommand(
"UPDATE Modules SET Available = @Available WHERE ModuleName = @ModuleName", conn);
cmd.Parameters.AddWithValue("@Available", false);
cmd.Parameters.AddWithValue("@ModuleName", moduleName);
conn.Open();
cmd.ExecuteNonQuery();
}
// Update DataGridView
row.Cells["Available"].Value = false; // Mark as booked
MessageBox.Show($"Module '{moduleName}' has been successfully booked!", "Booking Confirmation");
lblStatus.Text = "Status: Module has been booked";
}
else
{
MessageBox.Show("This module is not available for booking.", "Booking Error");
}
}
}
// Button click event to book the selected module
private void btnBook_Click(object sender, EventArgs e)
{
// This button click is primarily redundant due to in-cell booking.
// However, you can add further actions here if needed for your requirements.
MessageBox.Show("Select a module from the list to book", "Booking Information");
}
// Button click event to navigate back or to another form
private void btnBack_Click(object sender, EventArgs e)
{
// Close the current form (You can modify this for your navigation logic)
this.Close();
}
}
}