Vous êtes sur la page 1sur 4

using System;

using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace TpModeDéconnecté
{
public partial class MAJTDI201 : Form
{
public MAJTDI201()
{
InitializeComponent();
}
SqlConnection con = new SqlConnection(@"Data Source = DESKTOP-JUHAOL1\
SQLEXPRESS; Initial Catalog = Gst_Ventes; Integrated Security = True");
SqlDataAdapter dav;
SqlDataAdapter daclt;
DataSet ds = new DataSet();
private void MAJTDI201_Load(object sender, EventArgs e)
{
dav = new SqlDataAdapter("select * from villes", con);
daclt = new SqlDataAdapter("select * from clients", con);
dav.Fill(ds, "ville");
daclt.Fill(ds, "clt");
daclt.FillSchema(ds, SchemaType.Source, "clt");
dgvclient.DataSource = ds.Tables["clt"];
CmbVille.ValueMember = "CodeVille";
CmbVille.DisplayMember = "VilleNom";
CmbVille.DataSource = ds.Tables["ville"];

txtCode.DataBindings.Add("Text", ds.Tables["clt"],
"IdClt",true,DataSourceUpdateMode.Never);
txtnom.DataBindings.Add("Text", ds.Tables["clt"], "Nom", true,
DataSourceUpdateMode.Never);
txtprenom.DataBindings.Add("Text", ds.Tables["clt"], "Prenom", true,
DataSourceUpdateMode.Never);
txtadresse.DataBindings.Add("Text", ds.Tables["clt"], "Adresse", true,
DataSourceUpdateMode.Never);
txttel.DataBindings.Add("Text", ds.Tables["clt"], "Tel", true,
DataSourceUpdateMode.Never);
CmbVille.DataBindings.Add("selectedValue", ds.Tables["clt"], "Ville",
true, DataSourceUpdateMode.Never);

}
void naviguer(int i)
{
txtCode.DataBindings["Text"].BindingManagerBase.Position = i;

}
int pos = 0;
int rechercher(int code)
{
int i = -1;
foreach (DataRow r in ds.Tables["clt"].Rows)
{
if(r.RowState!=DataRowState.Deleted)
if ((int)r["IdClt"] == code)
{
i = ds.Tables["clt"].Rows.IndexOf(r);
break;
}

return i;
}
private void btnpremier_Click(object sender, EventArgs e)
{
pos = 0;
naviguer(pos);
}

private void btnsuivant_Click(object sender, EventArgs e)


{
if(pos<ds.Tables["clt"].Rows.Count-1)
{
pos++;
naviguer(pos);
}
}

private void btnprecedent_Click(object sender, EventArgs e)


{
if (pos > 0)
{
pos--;
naviguer(pos);
}
}

private void btndernier_Click(object sender, EventArgs e)


{

pos= ds.Tables["clt"].Rows.Count - 1;
naviguer(pos);

private void btnNouveau_Click(object sender, EventArgs e)


{
if(btnNouveau.Text=="Nouveau")
{
foreach (Control c in Controls)
{ if (c is TextBox)
c.ResetText();
if (c is ComboBox)
((ComboBox)c).SelectedIndex = -1;

}
btnNouveau.Text = "Ajouter";
}
else
{
btnNouveau.Text = "Nouveau";
if (txtCode.Text == "" || txtnom.Text == "")
MessageBox.Show("Veuillez renseigner tous les champs");
else
{
try
{
ds.Tables["clt"].Rows.Add(txtCode.Text, txtnom.Text,
txtprenom.Text, txtadresse.Text, txttel.Text, CmbVille.SelectedValue.ToString());
MessageBox.Show("bien ajouté");
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);

}
}

}
}

private void btnModifier_Click(object sender, EventArgs e)


{
int i = rechercher(int.Parse(txtCode.Text));
ds.Tables["clt"].Rows[i].BeginEdit();
ds.Tables["clt"].Rows[i][1] = txtnom.Text;
ds.Tables["clt"].Rows[i][2] = txtprenom.Text;
ds.Tables["clt"].Rows[i][3] = txtadresse.Text;
ds.Tables["clt"].Rows[i][4] = txttel.Text;
ds.Tables["clt"].Rows[i][5] = CmbVille.SelectedValue.ToString();

ds.Tables["clt"].Rows[i].EndEdit();
MessageBox.Show("bien modifié");
}

private void btnSupprimer_Click(object sender, EventArgs e)


{
int i = rechercher(int.Parse(txtCode.Text));
//remove ne supprime pas dans la base de données
//ds.Tables["clt"].Rows.RemoveAt(i);
ds.Tables["clt"].Rows[i].Delete();
MessageBox.Show("Bien supprimé");
}

private void btnEnregistrer_Click(object sender, EventArgs e)


{
//foreach (DataRow r in ds.Tables["clt"].Rows)
// MessageBox.Show(r.RowState.ToString());
SqlCommandBuilder cb = new SqlCommandBuilder(daclt);
daclt.Update(ds.Tables["clt"]);
MessageBox.Show("bien enregistré");
//foreach (DataRow r in ds.Tables["clt"].Rows)
// MessageBox.Show(r.RowState.ToString());

private void btnRechercher_Click(object sender, EventArgs e)


{
int p = rechercher(int.Parse(txtcoderech.Text));
if (p != -1)
{
pos = p;

}
else
{
MessageBox.Show("n 'existe pas");
pos = 0;
}
naviguer(pos);
}

private void groupBox1_Enter(object sender, EventArgs e)


{

private void btnrechSelect_Click(object sender, EventArgs e)


{
DataRow r = ds.Tables["clt"].Select("Idclt=" +
txtcoderech.Text).FirstOrDefault();
MessageBox.Show("Nom "+r["nom"].ToString());
}

private void CmbVille_SelectedIndexChanged(object sender, EventArgs e)


{
dataGridView1.Rows.Clear();
DataRow[] r = ds.Tables["clt"].Select("Ville=" +
CmbVille.SelectedValue);
// DataTable t = new DataTable();
//MessageBox.Show("Test "+r.Count());
if(r.Count()!=0)
for (int i = 0; i < r.Count(); i++)
dataGridView1.Rows.Add(r[i][0], r[i][1], r[i][2]);
}
}
}

Vous aimerez peut-être aussi