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 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556
| using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
namespace TestBDDCruas
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
#region variable pour la connection a la base de donnée
//déclaration de la connection
private OleDbConnection Conn = new OleDbConnection();
//déclaration du chemin de l'éxécutable
private string BDDPath = Application.ExecutablePath;
//déclaration du chemin de la connection de la base de donnée
private string ConnString;
#endregion
#region variables Connection pour ComboClass et listclass
//déclaration du dataset
private DataSet DtsClass = new DataSet();
//déclaration de la datatable
private DataTable DttClass = new DataTable();
//déclaration de la requete sql pour le comboclass
private string SqlClass = "SELECT * FROM Switchs";
#endregion
#region variables connection pour la datagrid et recherche
//déclaration du dataset
private DataSet DtsGrid = new DataSet();
//déclaration du datatable
private DataTable DttGrid = new DataTable();
//déclaration de la requete sql
private string SqlGrid = "SELECT * FROM Switchs";
//déclaration de la requete sql
private string SqlSearch;
//déclaration du commande builder
private OleDbCommandBuilder CmdGrid = new OleDbCommandBuilder();
//déclaration du datarow
private DataRow DtrGrid;
#endregion
#region variables
//Import de la class Variable
ClassVariable CsV = new ClassVariable();
#endregion
//-------------------------------------------------------------------------
private void Form1_Load(object sender, EventArgs e)
{
// TODO*: cette ligne de code charge les données dans la table 'cruasDataSet.Switchs'. Vous pouvez la déplacer ou la supprimer selon vos besoins.
this.switchsTableAdapter.Fill(this.cruasDataSet.Switchs);
#region Construction de la connection
//Arangement du chemin de la prise de la base de donnée
this.BDDPath = this.BDDPath.Substring(0, this.BDDPath.LastIndexOf("\\"));
//on crée le chemin de la connection a la base de donnée
this.ConnString = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + this.BDDPath + "\\Cruas.mdb";
//on crée la connection a la base de donnée
this.Conn.ConnectionString = this.ConnString;
#endregion
//on ouvre la connection
this.Conn.Open();
//---------------------------------------------------------------------------
#region comboLieu, listLieu; comboModele et listModele;
//déclaration du data adapter pour le combobox
OleDbDataAdapter Dta = new OleDbDataAdapter(this.SqlClass, this.Conn);
//on leve l'exception
try
{
//on rempli la dataset avec la table
Dta.Fill(this.DtsClass, "Switchs");
}
catch (Exception)
{
//affichage de l'érreur
MessageBox.Show("Une Erreur est survenue, si cela persiste veuillez contacter au plus vite le développeur ");
//on ferme la connection
this.Conn.Close();
//on ferme la fenêtre
this.Close();
//on quitte l'application
Application.Exit();
}
#endregion
//-----------------------------------------------------------------------
#region datagridview
//déclaration du data adapteur pour le datagridview
OleDbDataAdapter DtaGrid = new OleDbDataAdapter(this.SqlGrid, this.Conn);
//on leve l'exception
try
{
DtaGrid.Fill(this.DtsGrid, "Switchs");
}
catch (Exception)
{
//affichage de l'érreur
MessageBox.Show("Une Erreur est survenue, si cela persiste veuillez contacter au plus vite le programmeur");
//on ferme la connection
this.Conn.Close();
//on quitte l'application
Application.Exit();
}
//On rempli la liste avec le dataset
this.DttGrid = this.DtsGrid.Tables["Switchs"];
//on affiche la liste dans le datagridview
this.Dtg.DataSource = this.DttGrid;
//On stylise la datagridview
this.StyleDtg();
#endregion
//Rafraichissement de la fenetre
this.Show();
//On ferme la connection
this.Conn.Close();
}
#region buttons
private void ButtonSave_Click(object sender, EventArgs e)
{
//On vérifie que tout les champs sont remplis
if (this.TxtNumeroPI.Text == null || this.TxtNumeroPI.Text == null || this.comboModele.Text == null || this.comboLieu.Text == null)
{
MessageBox.Show ( "Veuillez remplir tout les champs" );
}
else
{
//déclaration du data adapter
OleDbDataAdapter Dta = new OleDbDataAdapter ( this.SqlGrid , this.Conn );
//création d'une nouvelle ligne
this.DtrGrid = this.DtsGrid.Tables [ "Switchs" ].NewRow ();
//remplissage des champs
this.DtrGrid [ "N°" ] = this.DtsGrid.Tables [ "Switchs" ].Rows.Count + 1;
this.DtrGrid [ "NumeroPI" ] = this.CsV.NumeroPI;
this.DtrGrid [ "AdresseMAC" ] = this.CsV.AdresseMAC;
this.DtrGrid [ "Modele" ] = this.CsV.Modele;
this.DtrGrid ["Lieu"] = this.CsV.Lieu;
//ajout de la ligne à la table
this.DtsGrid.Tables [ "Switchs" ].Rows.Add ( DtrGrid );
//on met tout dans la base de donnée
this.CmdGrid = new OleDbCommandBuilder ( Dta );
Dta.UpdateCommand = CmdGrid.GetUpdateCommand ();
Dta.Update ( DtsGrid , "Switchs" );
this.TxtNumeroPI.Text = null;
this.TxtAdresseMAC.Text = null;
this.comboModele.Text = null;
this.comboLieu.Text = null;
}
}
private void buttonModif_Click(object sender, EventArgs e)
{
if (this.CsV.Ligne < this.DtsGrid.Tables["Switchs"].Rows.Count)
{
if (this.CsV.Ligne >= 0)
{
//on charge les données dans les variables
this.CsV.NumeroPI = this.DtsGrid.Tables["Switchs"].Rows[this.CsV.Ligne]["NumeroPI"].ToString();
this.CsV.AdresseMAC = this.DtsGrid.Tables["Switchs"].Rows[this.CsV.Ligne]["AdresseMAC"].ToString();
this.CsV.Modele = this.DtsGrid.Tables["Switchs"].Rows[this.CsV.Ligne]["Modele"].ToString();
this.CsV.Lieu = this.DtsGrid.Tables["Switchs"].Rows[this.CsV.Ligne]["Lieu"].ToString();
//on met les données des variables dans les textbox
this.TxtNumeroPI.Text = this.CsV.NumeroPI;
this.TxtAdresseMAC.Text = this.CsV.AdresseMAC;
this.comboModele.Text = this.CsV.Modele;
this.comboLieu.Text = this.CsV.Lieu;
}
else
{
MessageBox.Show("Il n'y a pas d'enregistrements");
}
}
}
private void buttonUpdateModif_Click(object sender, EventArgs e)
{
//déclaration du data adapter
OleDbDataAdapter Dta = new OleDbDataAdapter ( this.SqlGrid , this.Conn );
//on leve l'exception
try
{
//on prend la ligne en cour
this.DtrGrid = this.DtsGrid.Tables [ "Switchs" ].Rows [ this.CsV.Ligne ];
}
catch ( IndexOutOfRangeException )
{
this.CsV.Ligne = 0;
}
//on vérifie que les txtbox ne sont pas vides
if ( TxtNumeroPI.Text == null || TxtAdresseMAC.Text == null )
{
this.CsV.Ligne = 0;
}
else
{
//on rempli les champs
this.DtrGrid [ "NumeroPI" ] = this.CsV.NumeroPI;
this.DtrGrid [ "AdresseMAC" ] = this.CsV.AdresseMAC;
this.DtrGrid [ "Modele" ] = this.CsV.Modele;
this.DtrGrid ["Lieu"] = this.CsV.Lieu;
}
//on leve l'exception
try
{
//on met tout dans la base de donnée
this.CmdGrid = new OleDbCommandBuilder ( Dta );
Dta.UpdateCommand = CmdGrid.GetUpdateCommand ();
Dta.Update ( DtsGrid , "Switchs" );
}
catch (DBConcurrencyException /*DBCEx*/ )
{
MessageBox.Show ( "Vous ne pouvez pas modifier quelque chose que vous venez de sauvegarder, veuillez redémarer l'application et recommencer" );
}
TxtNumeroPI.Text = null;
TxtAdresseMAC.Text = null;
comboModele.Text = null;
comboLieu.Text = null;
}
private void buttonAfter_Click(object sender, EventArgs e)
{
//on défini le nombre de lignes
this.CsV.NbSauvegarde = this.DtsGrid.Tables["Switchs"].Rows.Count;
//si il n'y a rien dans les textbox alors il faut aller a la premiere ligne
if (this.TxtNumeroPI.Text == null || this.TxtAdresseMAC.Text == null || this.comboModele.Text == null || this.comboLieu.Text == null)
{
this.CsV.Ligne = 0;
}
else
{
//on saute une ligne a chaques click
this.CsV.Ligne = this.CsV.Ligne + 1;
//on ne dépasse pas le dernier enregistrement
if (this.CsV.Ligne > this.CsV.NbSauvegarde - 1)
{
this.CsV.Ligne = this.CsV.NbSauvegarde - 1;
}
}
//on vérifi qu'il y a un enregistrement
if ( this.CsV.Ligne < 0 )
{
this.CsV.Ligne = 0;
}
else
{
//on charge les données dans les variables
this.CsV.NumeroPI = this.DttGrid.Rows [ this.CsV.Ligne ] [ "NumeroPI" ].ToString ();
this.CsV.AdresseMAC = this.DttGrid.Rows [ this.CsV.Ligne ] [ "AdresseMAC" ].ToString ();
this.CsV.Modele = this.DttGrid.Rows [ this.CsV.Ligne ] [ "modele" ].ToString ();
this.CsV.Lieu = this.DttGrid.Rows[this.CsV.Ligne]["Lieu"].ToString();
//on charge les données dans les textbox
this.TxtNumeroPI.Text = this.CsV.NumeroPI;
this.TxtAdresseMAC.Text = this.CsV.AdresseMAC;
this.comboModele.Text = this.CsV.Modele;
this.comboLieu.Text = this.CsV.Lieu;
}
}
private void buttonBefore_Click(object sender, EventArgs e)
{
//si il n'y a rien dans les textbox, alors il faut aller à la première ligne
if (this.TxtNumeroPI.Text == null || this.TxtAdresseMAC.Text == null || this.comboModele.Text == null || this.comboLieu.Text == null)
{
this.CsV.Ligne = 0;
}
else
{
// On saute une ligne a chaques click
this.CsV.Ligne = this.CsV.Ligne - 1;
}
// On vérifie qu'il y à un enregistrement et,qu'on ne dépasse pas le premier enregistrement
if ( this.CsV.Ligne < 0 )
{
this.CsV.Ligne = 0;
}
else
{
//on charge les données dans les variables
this.CsV.NumeroPI = this.DttGrid.Rows [ this.CsV.Ligne ] [ "NumeroPI" ].ToString ();
this.CsV.AdresseMAC = this.DttGrid.Rows [ this.CsV.Ligne ] [ "AdresseMAC" ].ToString ();
this.CsV.Modele = this.DttGrid.Rows [ this.CsV.Ligne ] [ "Modele" ].ToString ();
this.CsV.Lieu = this.DttGrid.Rows[this.CsV.Ligne]["Lieu"].ToString();
//On charge les données dans les textbox
this.TxtNumeroPI.Text = this.CsV.NumeroPI;
this.TxtAdresseMAC.Text = this.CsV.AdresseMAC;
this.comboModele.Text = this.CsV.Modele;
this.comboLieu.Text = this.CsV.Lieu;
}
}
private void buttonDelete_Click(object sender, EventArgs e)
{
// Déclaration du data adapter
OleDbDataAdapter Dta = new OleDbDataAdapter(this.SqlGrid, this.Conn);
//on vérifi qu'il y a un enregistrement
if (this.CsV.Ligne < 0)
{
this.CsV.Ligne = 0;
}
// On vérifie qu'il y à quelque chose dans les textbox
else if (this.TxtNumeroPI.Text == null || this.TxtAdresseMAC.Text == null)
{
this.CsV.Ligne = 0;
}
else
{
// On prend la ligne en cours et on la supprime
this.DtsGrid.Tables["Switchs"].Rows[this.CsV.Ligne].Delete();
}
//on va a la ligne précédente
this.CsV.Ligne = this.CsV.Ligne - 1;
// On ne dépasse pas le dernier enregistrement
if (this.CsV.Ligne < 0)
{
this.CsV.Ligne = 0;
}
// On lève l'exception
try
{
// On met à jour la base de donnée
this.CmdGrid = new OleDbCommandBuilder(Dta);
Dta.UpdateCommand = CmdGrid.GetUpdateCommand();
Dta.Update(DtsGrid, "Switchs");
}
catch (DBConcurrencyException)
{
MessageBox.Show("Vous ne pouvez pas modifier quelque chose que vous venez de sauvegarder, veuillez redémarer l'application et recommencer");
}
// On suprime les données dans les textbox
TxtNumeroPI.Text = "";
TxtAdresseMAC.Text = "";
comboModele.Text = "";
comboLieu.Text = "";
}
private void buttonExit_Click(object sender, EventArgs e)
{
this.Conn.Close();
this.Close();
Application.Exit();
}
private void buttonsearch_Click(object sender, EventArgs e)
{
// On vide le dataset
this.DtsGrid.Clear();
// On vérifie qu'il y à quelque chose dans la txtsearch
if (this.TxtSearch.Text == null)
{
// On choisi la requete sql
this.SqlSearch = this.SqlGrid;
}
else if (this.RadNumeroPI.Checked == true)
{
// On choisi la requete sql
this.SqlSearch = "SELECT Switchs.N°,Switchs.NumeroPI, Switchs.AdresseMAC, Switchs.Modele, Switchs.Lieu FROM Switchs WHERE (Switchs.NumeroPI LIKE '%" + this.CsV.Recherche + "%')";
}
else if (this.radAdresseMAC.Checked == true)
{
// On choisi la requete sql
this.SqlSearch = "SELECT Contact.N°,Switchs.NumeroPI,Switchs.AdresseMAC, Switchs.Modele, Switchs.Lieu FROM Switchs WHERE (Switchs.AdresseMAC LIKE '%" + this.CsV.Recherche + "%')";
}
else if (this.radModele.Checked == true)
{
// On choisi la requete sql
this.SqlSearch = "SELECT Switchs.N°,Switchs.NumeroPI, Switchs.AdresseMAC, Switchs.Modele, Switchs.Lieu FROM Switchs WHERE (Switchs.Modele LIKE '%" + this.CsV.Recherche + "%')";
}
else if (this.radLieu.Checked == true)
{
// On choisi la requete sql
this.SqlSearch = "SELECT Switchs.N°,Switchs.NumeroPI, Switchs.AdresseMAC, Switchs.Modele, Switchs.Lieu FROM Switchs WHERE (Switchs.Lieu LIKE '%" + this.CsV.Recherche + "%')";
}
else if (this.radTout.Checked == true)
{
// On choisi la requete sql
this.SqlSearch = "SELECT Switchs.N°,Switchs.NumeroPI, Switchs.AdresseMAC, Switchs.Modele, Switchs.Lieu FROM Switchs" ;
}
else if (this.RadNumeroPI.Checked == false && this.radAdresseMAC.Checked == false && this.radModele.Checked == false)
{
// On choisi la requete sql
this.SqlSearch = this.SqlGrid;
// On met un message d'alerte
MessageBox.Show("Veuillez choisir un critère de sélection dans la base de donnée pour la recherche", "Un oublie", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
//on choisi la requete sql
this.SqlSearch = this.SqlGrid;
}
//on ouvre la connection
this.Conn.Open();
//déclaration du data adapter
OleDbDataAdapter Dta = new OleDbDataAdapter(this.SqlSearch, this.Conn);
//on leve l'exception
try
{
// On rempli le dataset avec la table
Dta.Fill(this.DtsGrid, "Switchs");
}
catch (Exception)
{
//affichage de l'erreur
MessageBox.Show("Une Erreur est survenue, si cela persiste veuillez contacter au plus vite le programmeur");
//on ferme la connection
this.Conn.Close();
//on ferme la fenêtre
this.Close();
//on quitte l'application
Application.Exit();
}
//on rempli la liste du datagridview avec le dataset
this.DttGrid = this.DtsGrid.Tables["Switchs"];
//on affiche la liste du datagridview dans le datagridview
this.Dtg.DataSource = this.DttGrid;
//on stylise la datagridview
this.StyleDtg();
//rafraichissement de la fenetre
this.Show();
//on ferme la connection
this.Conn.Close();
//on vide la TxtSearch pour commencer une nouvelle recherche
this.TxtSearch.Text = null;
}
#endregion
#region Fonctions
private void StyleDtg()
{
this.Dtg.ColumnHeadersDefaultCellStyle.Font = new Font("Tahoma", 9, FontStyle.Bold, GraphicsUnit.Point);
this.Dtg.ColumnHeadersDefaultCellStyle.BackColor = SystemColors.ControlDark;
this.Dtg.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
this.Dtg.ColumnHeadersBorderStyle = DataGridViewHeaderBorderStyle.Single;
this.Dtg.DefaultCellStyle.Font = new Font("Tahoma", 8, FontStyle.Regular, GraphicsUnit.Point);
this.Dtg.DefaultCellStyle.BackColor = Color.Empty;
this.Dtg.AlternatingRowsDefaultCellStyle.BackColor = SystemColors.ControlLight;
this.Dtg.CellBorderStyle = DataGridViewCellBorderStyle.Single;
this.Dtg.GridColor = SystemColors.ControlDarkDark;
Dtg.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells);
}
#endregion
#region TextBox
//TxtNom_TextChanged
private void TxtNumeroPI_TextChanged(object sender, EventArgs e)
{
this.CsV.NumeroPI = this.TxtNumeroPI.Text;
}
//TxtAdresseMAC_TextChanged
private void TxtAdresseMAC_TextChanged(object sender, EventArgs e)
{
this.CsV.AdresseMAC = this.TxtAdresseMAC.Text;
}
// comboModele_SelectedIndexChanged
private void comboModele_SelectedIndexChanged(object sender, EventArgs e)
{
this.CsV.Modele = this.comboModele.Text;
}
private void comboLieu_SelectedIndexChanged(object sender, EventArgs e)
{
this.CsV.Lieu = this.comboLieu.Text;
}
private void TxtSearch_TextChanged(object sender, EventArgs e)
{
this.CsV.Recherche = this.TxtSearch.Text;
}
#endregion
}
} |
Partager