-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathForm1.cs
179 lines (144 loc) · 5.87 KB
/
Form1.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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml.Linq;
using TheArtOfDevHtmlRenderer.Adapters;
namespace gestion_commercial_last_episode
{
public partial class Form1 : Form
{
private connexion myConnection;
public Form1()
{
InitializeComponent();
myConnection = new connexion(); // Instantiate Connexion class
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void ChargerDonnees()
{
string query = "SELECT * FROM Clients";
SqlCommand cmd = new SqlCommand(query, myConnection.GetConnection());
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
DataTable dataTable = new DataTable();
adapter.Fill(dataTable);
guna2DataGridView1.DataSource = dataTable;
}
private void ClearAll()
{
txtName.Clear();
txtAdresse.Clear();
txtCp.Clear();
txtVille.Clear();
}
private void btnAdd_Click(object sender, EventArgs e)
{
try
{
myConnection.OpenConnection();
string query = "INSERT INTO Clients (Nom, Adresse, Cp, Ville) VALUES (@Nom, @Adresse, @Cp, @Ville)";
using (SqlCommand cmd = new SqlCommand(query, myConnection.GetConnection()))
{
cmd.Parameters.AddWithValue("@Nom", txtName.Text);
cmd.Parameters.AddWithValue("@Adresse", txtAdresse.Text);
cmd.Parameters.AddWithValue("@Cp", txtCp.Text);
cmd.Parameters.AddWithValue("@Ville", txtVille.Text);
cmd.ExecuteNonQuery();
ChargerDonnees();
ClearAll();
}
MessageBox.Show("Client added successfully!!! ;)");
}
catch (Exception ex)
{
MessageBox.Show("Error adding client: -_- " + ex.Message);
}
finally
{
myConnection.CloseConnection();
}
}
int id;
private void guna2DataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
id = int.Parse(guna2DataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString());
txtName.Text = guna2DataGridView1.SelectedRows[0].Cells[1].Value.ToString();
txtAdresse.Text = guna2DataGridView1.SelectedRows[0].Cells[2].Value.ToString();
txtCp.Text = guna2DataGridView1.SelectedRows[0].Cells[3].Value.ToString();
txtVille.Text = guna2DataGridView1.SelectedRows[0].Cells[4].Value.ToString();
ChargerDonnees();
ClearAll();
}
private void Form1_Load_1(object sender, EventArgs e)
{
// Open the database connection when the form loads
myConnection.OpenConnection();
ChargerDonnees();
ClearAll();
}
private void btnEdit_Click(object sender, EventArgs e)
{
if (guna2DataGridView1.SelectedRows.Count > 0)
{
// Récupérez l'ID de la ligne sélectionnée (assurez-vous d'avoir une colonne d'ID dans votre DataGridView)
int selectedRowIndex = guna2DataGridView1.SelectedRows[0].Index;
int selectedClientId = Convert.ToInt32(guna2DataGridView1["id", selectedRowIndex].Value);
string query = "UPDATE Clients SET Nom = @Nom, Adresse = @Adresse, Cp = @Cp, Ville = @Ville WHERE Id = @Id";
SqlCommand command = new SqlCommand(query, myConnection.GetConnection());
command.Parameters.AddWithValue("@Nom", txtName.Text);
command.Parameters.AddWithValue("@Adresse", txtAdresse.Text);
command.Parameters.AddWithValue("@Cp", txtCp.Text);
command.Parameters.AddWithValue("@Ville", txtVille.Text);
command.Parameters.AddWithValue("@Id", selectedClientId);
command.ExecuteNonQuery();
ChargerDonnees();
ClearAll();
}
}
private void btnDelete_Click(object sender, EventArgs e)
{
}
private void guna2DataGridView1_SelectionChanged(object sender, EventArgs e)
{
if (guna2DataGridView1.SelectedRows.Count > 0)
{
DataGridViewRow selectedRow = guna2DataGridView1.SelectedRows[0];
// Les données sont stockées dans les cellules de la ligne sélectionnée
string Nom = selectedRow.Cells["Nom"].Value.ToString();
string Adresse = selectedRow.Cells["Adresse"].Value.ToString();
string Cp = selectedRow.Cells["Cp"].Value.ToString();
string Ville = selectedRow.Cells["Ville"].Value.ToString();
// Afficher les données dans les TextBox
txtName.Text = Nom;
txtAdresse.Text = Adresse;
txtCp.Text = Cp;
txtVille.Text = Ville;
}
}
private void btnRefresh_Click(object sender, EventArgs e)
{
ChargerDonnees();
ClearAll();
}
private void guna2CircleButton1_Click(object sender, EventArgs e)
{
Application.Exit();
ClearAll();
}
private void btnClearFields_Click(object sender, EventArgs e)
{
ClearAll();
}
private void guna2DataGridView1_CellContentClick_1(object sender, DataGridViewCellEventArgs e)
{
}
}
}