Mar.27
TP14 : Les boutons de navigation en mode déconnecté
Objectif
Dans ce TP vous allez apprendre comme créer les boutons de navigation en mode déconnecté.
Questions
Partie serveur :
- Créer la base imdb.
- Dans la base imdb, crée les tables genre et serie :

Partie client :
- Réaliser un menu qui facilite l’utilisation de l’application.
- Créer l’interface suivante :

- Ecrire le code qui va permettre d’affiche tous les enregistrements de la table genre sur DataGridView.
- Ecrire les code des boutons Ajouter, Modifier, Supprimer et Vider.
- Ecrire les code des boutons de navigation des boutons Suivant, Précédent, Premier et Dernier.
- Ecrire le code pour le Bouton Enregistre qui permet de valider les mises à jour sur la table genre.
- Créer l’interface suivante :

- Ecrire le code qui va permettre d’affiche tous les enregistrements de la table serie sur
- Ecrire les code des boutons Ajouter, Modifier, Supprimer et Vider.
- Ecrire les code des boutons de navigation des boutons Suivant, Précédent, Premier et Dernier.
- Ecrire le code pour le Bouton Enregistre qui permet de valider les mises à jour sur la table serie.
Correction
La classe Genre
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace TP15_IMDB
{
public partial class Genre : Form
{
SqlConnection conn = new SqlConnection("server = DESKTOP-EIALG0J\\SQLEXPRESS; database = imdb; integrated security = SSPI");
DataSet ds = new DataSet();
SqlCommand cmd = new SqlCommand();
SqlDataAdapter da = new SqlDataAdapter();
int postion = 0;
public Genre()
{
InitializeComponent();
}
private void Genre_Load(object sender, EventArgs e)
{
conn.Open();
SqlDataAdapter ad = new SqlDataAdapter("select * from genre", conn);
ad.Fill(ds, "genre");
dv.DataSource = ds.Tables["genre"];
DataColumn[] PK_Four = new DataColumn[1];
PK_Four[0] = ds.Tables["genre"].Columns[0];
ds.Tables["genre"].PrimaryKey = PK_Four;
conn.Close();
}
private void button8_Click(object sender, EventArgs e)
{
intituleGenre.Text = "";
codeGenre.Text = "";
}
private void button5_Click(object sender, EventArgs e)
{
DataRow ligne = ds.Tables[0].Rows.Find(codeGenre.Text);
if (ligne == null)
{
DataRow nouvelleLigne = ds.Tables[0].NewRow();
nouvelleLigne[0] = codeGenre.Text;
nouvelleLigne[1] = intituleGenre.Text;
ds.Tables[0].Rows.Add(nouvelleLigne);
}
else
{
MessageBox.Show("Le genre déja existant");
}
}
private void button6_Click(object sender, EventArgs e)
{
DataRow ligne = ds.Tables[0].Rows.Find(codeGenre.Text);
if (ligne != null)
{
ligne.BeginEdit();
ligne[0] = codeGenre.Text;
ligne[1] = intituleGenre.Text;
ligne.EndEdit();
}
else
{
MessageBox.Show("Le genre est introuvable");
}
}
private void button7_Click(object sender, EventArgs e)
{
DataRow ligne = ds.Tables[0].Rows.Find(codeGenre.Text);
if (ligne != null)
{
ligne.Delete();
}
else
{
MessageBox.Show("Le genre est introuvable");
}
}
private void button9_Click(object sender, EventArgs e)
{
conn.Open();
SqlDataAdapter ad = new SqlDataAdapter("select * from genre", conn);
SqlCommandBuilder sqb = new SqlCommandBuilder(ad);
ad.Update(ds, "genre");
conn.Close();
}
private void premier_Click(object sender, EventArgs e)
{
postion = 0;
navigation(postion);
}
private void navigation(int postion)
{
codeGenre.Text = ds.Tables[0].Rows[postion][0].ToString();
intituleGenre.Text = ds.Tables[0].Rows[postion][1].ToString();
}
private void dernier_Click(object sender, EventArgs e)
{
postion = ds.Tables["genre"].Rows.Count - 1; // Positioner le curseur dans la fin
navigation(postion);
}
private void precedent_Click(object sender, EventArgs e)
{
try
{
postion -= 1;// Positioner le curseur dans la position précédente
navigation(postion);
}
catch (IndexOutOfRangeException)
{
MessageBox.Show("Vous avez atteint le début");
postion = 0;
}
}
private void suivant_Click(object sender, EventArgs e)
{
try
{
postion += 1;// Positioner le curseur dans la position suivante
navigation(postion);
}
catch (IndexOutOfRangeException)
{
MessageBox.Show("Vous avez atteint la fin");
postion = ds.Tables["genre"].Rows.Count - 1;
}
}
private void codeGenre_TextChanged(object sender, EventArgs e)
{
}
private void label2_Click(object sender, EventArgs e)
{
}
private void label1_Click(object sender, EventArgs e)
{
}
private void intituleGenre_TextChanged(object sender, EventArgs e)
{
}
}
}
La classe Serie
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace TP15_IMDB
{
public partial class Serie : Form
{
SqlConnection conn = new SqlConnection("server = DESKTOP-EIALG0J\\SQLEXPRESS; database = imdb; integrated security = SSPI");
DataSet ds = new DataSet();
SqlCommand cmd = new SqlCommand();
SqlDataAdapter da = new SqlDataAdapter();
int postion = 0;
public Serie()
{
InitializeComponent();
}
private void Serie_Load(object sender, EventArgs e)
{
conn.Open();
SqlDataAdapter ad1 = new SqlDataAdapter("select * from serie", conn);
ad1.Fill(ds, "serie");
SqlDataAdapter ad2 = new SqlDataAdapter("select * from genre", conn);
ad2.Fill(ds, "genre");
DataColumn[] PK_Serie = new DataColumn[1];
PK_Serie[0] = ds.Tables["serie"].Columns[0];
ds.Tables["serie"].PrimaryKey = PK_Serie;
DataColumn[] PK_Genre = new DataColumn[1];
PK_Genre[0] = ds.Tables["genre"].Columns[0];
ds.Tables["genre"].PrimaryKey = PK_Genre;
ds.Relations.Add("Rel_Gen_Ser", ds.Tables["genre"].Columns[0], ds.Tables["serie"].Columns[0]);
conn.Close();
genreSerie.DataSource = ds.Tables["genre"];
genreSerie.DisplayMember = ds.Tables["genre"].Columns[1].ColumnName;
genreSerie.ValueMember = ds.Tables["genre"].Columns[0].ColumnName;
genreSerie.SelectedIndex = -1;
dv.DataSource = ds.Tables["serie"];
conn.Close();
}
private void vider_Click(object sender, EventArgs e)
{
codeSerie.Text = "";
titreSerie.Text = "";
genreSerie.SelectedIndex = -1;
}
private void supprimer_Click(object sender, EventArgs e)
{
DataRow ligne = ds.Tables[1].Rows.Find(codeSerie.Text);
if (ligne != null)
{
ligne.Delete();
}
else
{
MessageBox.Show("La série n'existe pas");
}
}
private void ajouter_Click(object sender, EventArgs e)
{
DataRow ligne = ds.Tables[0].Rows.Find(codeSerie.Text);
if (ligne == null)
{
DataRow nouvelleLigne = ds.Tables[0].NewRow();
nouvelleLigne[0] = codeSerie.Text;
nouvelleLigne[1] = titreSerie.Text;
nouvelleLigne[2] = dateSortie.Value.ToString() ;
nouvelleLigne[3] = genreSerie.SelectedValue;
ds.Tables[0].Rows.Add(nouvelleLigne);
}
else
{
MessageBox.Show("La série déja existante");
}
}
private void modifier_Click(object sender, EventArgs e)
{
DataRow ligne = ds.Tables[0].Rows.Find(codeSerie.Text);
if (ligne != null)
{
ligne.BeginEdit();
ligne[1] = titreSerie.Text;
ligne[2] = dateSortie.Value.ToString();
ligne[3] = genreSerie.SelectedValue;
ligne.EndEdit();
}
else
{
MessageBox.Show("La série n'existe pas");
}
}
private void premier_Click(object sender, EventArgs e)
{
postion = 0;
navigation(postion);
}
private void navigation(int postion)
{
codeSerie.Text = ds.Tables[0].Rows[postion][0].ToString();
titreSerie.Text = ds.Tables[0].Rows[postion][1].ToString();
dateSortie.Text = ds.Tables[0].Rows[postion][2].ToString();
genreSerie.SelectedIndex = int.Parse(ds.Tables[0].Rows[postion][3].ToString());
}
private void dernier_Click(object sender, EventArgs e)
{
postion = ds.Tables["serie"].Rows.Count - 1; // Positioner le curseur dans la fin
navigation(postion);
}
private void precedent_Click(object sender, EventArgs e)
{
try
{
postion -= 1;// Positioner le curseur dans la position précédente
navigation(postion);
}
catch (IndexOutOfRangeException)
{
MessageBox.Show("Vous avez atteint le début");
postion = 0;
}
}
private void suivant_Click(object sender, EventArgs e)
{
try
{
postion += 1;// Positioner le curseur dans la position suivante
navigation(postion);
}
catch (IndexOutOfRangeException)
{
MessageBox.Show("Vous avez atteint la fin");
postion = ds.Tables["genre"].Rows.Count - 1;
}
}
}
}
