Vous êtes sur la page 1sur 7

DataSet DS_Livres = new DataSet();

SqlConnection Cnx;
SqlDataAdapter DA;
SqlCommandBuilder CB;
int iPos=-1;
string sConnexion = "Data Source=.;Initial Catalog=ADO_DB;Integrated Security=True";
string sQuery;
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)


{
sQuery = "select * From MD_Livre";
Cnx = new SqlConnection();
DA = new SqlDataAdapter();
Cnx.ConnectionString = sConnexion;
DA.SelectCommand = Cnx.CreateCommand();
DA.SelectCommand.CommandText = sQuery;

DA.Fill(DS_Livres, "Livres");

Fill_ComboBox();

private void Fill_ComboBox()


{
cmb_Code.Items.Clear();
foreach (DataRow rw in DS_Livres.Tables["Livres"].Rows)
{
cmb_Code.Items.Add(rw[0]);
}
}

private void Show_Livre_Data(int Ps)


{
DataRow Rw = DS_Livres.Tables["Livres"].Rows[Ps];
cmb_Code.Text = Rw[0].ToString();
txt_Titre.Text = Rw[1].ToString();
txt_Auteur.Text = Rw[2].ToString();
txt_NbrExp.Text = Rw[3].ToString();
}

private void cmb_Code_SelectedIndexChanged(object sender, EventArgs e)


{
if (cmb_Code.SelectedIndex != -1)
{
iPos = cmb_Code.SelectedIndex;
Show_Livre_Data(iPos);
}
}

private void button1_Click(object sender, EventArgs e)


{
cmb_Code.SelectedIndex = -1;
cmb_Code.Text = txt_Titre.Text = txt_Auteur.Text = txt_NbrExp.Text = string.Empty;
}

private void button2_Click(object sender, EventArgs e)


{
try
{
DataRow rLivre = DS_Livres.Tables["Livres"].NewRow();
int iCode,iNbrExp;
if (int.TryParse(cmb_Code.Text,out iCode) && !string.IsNullOrEmpty(txt_Titre.Text)
&& !string.IsNullOrEmpty(txt_Auteur.Text) && int.TryParse(txt_NbrExp.Text,out
iNbrExp))
{
bool bFound = false;
foreach (DataRow rw in DS_Livres.Tables["Livres"].Rows)
{
if (int.Parse(rw[0].ToString()) == iCode)
{
bFound = true;
}
}
if (!bFound)
{
rLivre[0] = iCode;
rLivre[1] = txt_Titre.Text;
rLivre[2] = txt_Auteur.Text;
rLivre[3] = iNbrExp;
DS_Livres.Tables["Livres"].Rows.Add(rLivre);
}
else
MessageBox.Show("Ce Code - " + iCode.ToString() + " - Deja Existe ",
"Attention", MessageBoxButtons.OK, MessageBoxIcon.Warning);

}
}
catch (Exception Ex)
{
MessageBox.Show(Ex.Message, "Attention", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
finally
{
Fill_ComboBox();
button1.PerformClick();
}
}

private void button3_Click(object sender, EventArgs e)


{
try
{
if (cmb_Code.SelectedIndex != -1)
{
DataRow rLivre = DS_Livres.Tables["Livres"].Rows[iPos];
int iCode, iNbrExp;
if (int.TryParse(cmb_Code.Text, out iCode) && !string.IsNullOrEmpty(txt_Titre.Text)
&& !string.IsNullOrEmpty(txt_Auteur.Text) && int.TryParse(txt_NbrExp.Text, out
iNbrExp))
{
rLivre[1] = txt_Titre.Text;
rLivre[2] = txt_Auteur.Text;
rLivre[3] = iNbrExp;
}
}
else
MessageBox.Show("Vous devez choisir un livre de la liste deroulante", "Attention",
MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
catch (Exception Ex)
{
MessageBox.Show(Ex.Message, "Attention", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
finally
{
button1.PerformClick();
}
}

private void button4_Click(object sender, EventArgs e)


{
try
{
if (cmb_Code.SelectedIndex != -1)
{
int iCode;
if (int.TryParse(cmb_Code.Text, out iCode))
{
DS_Livres.Tables["Livres"].Rows[iPos].Delete();
}
}
else
MessageBox.Show("Vous devez choisir un livre de la liste deroulante", "Attention",
MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
catch (Exception Ex)
{
MessageBox.Show(Ex.Message, "Attention", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
finally
{
Fill_ComboBox();
button1.PerformClick();
}
}

private void button5_Click(object sender, EventArgs e)


{
Form2 frm = new Form2();

frm.dataGridView1.DataSource = DS_Livres.Tables["Livres"];
frm.ShowDialog();
}

private void button6_Click(object sender, EventArgs e)


{
iPos = 0;
Show_Livre_Data(iPos);
}

private void button7_Click(object sender, EventArgs e)


{
iPos--;
if (iPos < 0) iPos = 0;
Show_Livre_Data(iPos);
}

private void button8_Click(object sender, EventArgs e)


{
iPos++;
if (iPos > DS_Livres.Tables["Livres"].Rows.Count - 1) iPos =
DS_Livres.Tables["Livres"].Rows.Count - 1;
Show_Livre_Data(iPos);
}

private void button9_Click(object sender, EventArgs e)


{
iPos = DS_Livres.Tables["Livres"].Rows.Count - 1;
Show_Livre_Data(iPos);
}

private void Form1_FormClosed(object sender, FormClosedEventArgs e)


{
CB = new SqlCommandBuilder(DA);
DA.Update(DS_Livres, "Livres");
}

=======================================================================================================
=======================================================================================================
=======================================================================================================
=======================================================================================================

DataSet DS_Adherent = new DataSet();


SqlConnection Cnx;
SqlDataAdapter DA;
SqlCommandBuilder CB;
BindingSource myBindingSource;
string sConnexion = "Data Source=.;Initial Catalog=ADO_DB;Integrated Security=True";
string sQuery;

public Form4()
{
InitializeComponent();
}

private void Form4_Load(object sender, EventArgs e)


{
sQuery = "select * From MD_Adherent";
Cnx = new SqlConnection();
Cnx.ConnectionString = sConnexion;
DA = new SqlDataAdapter(sQuery, Cnx);

DA.Fill(DS_Adherent, "Adherent");

Navigate();
}

private void Navigate()


{
myBindingSource = new BindingSource();
myBindingSource.DataSource = DS_Adherent;
myBindingSource.DataMember = DS_Adherent.Tables["Adherent"].TableName;

dataGridView1.DataSource = myBindingSource;
textBox1.DataBindings.Add("Text", myBindingSource, "CodeA");
textBox2.DataBindings.Add("Text", myBindingSource, "NomA");
textBox3.DataBindings.Add("Text", myBindingSource, "adresse");
dateTimePicker1.DataBindings.Add("Text", myBindingSource, "DateInscription");
}
private void button1_Click(object sender, EventArgs e)
{
if (button1.Text == "Nouveau")
{
button1.Text = "Ajouter";
myBindingSource.AddNew();
}
else if (button1.Text == "Ajouter")
{
myBindingSource.EndEdit();
dataGridView1.Refresh();
button1.Text = "Nouveau";
}
}
private void button2_Click(object sender, EventArgs e)
{
myBindingSource.EndEdit();
}

private void button3_Click(object sender, EventArgs e)


{
myBindingSource.RemoveCurrent();
}

private void button6_Click(object sender, EventArgs e)


{
myBindingSource.MoveFirst();
}

private void button7_Click(object sender, EventArgs e)


{
myBindingSource.MovePrevious();
}

private void button8_Click(object sender, EventArgs e)


{
myBindingSource.MoveNext();
}

private void button9_Click(object sender, EventArgs e)


{
myBindingSource.MoveLast();
}

private void button5_Click(object sender, EventArgs e)


{
CB = new SqlCommandBuilder(DA);
DA.Update(DS_Adherent, "Adherent");
Application.Exit();
}

private void button4_Click(object sender, EventArgs e)


{
int foundIndex = myBindingSource.Find("CodeA", int.Parse(textBox4.Text));
if (foundIndex > -1)
myBindingSource.Position = foundIndex;
else
{
MessageBox.Show("Ce Code n'existe pas");
textBox4.Select(0,textBox4.Text.Length);
textBox4.Focus();
}
}

=======================================================================================================
=======================================================================================================
=======================================================================================================
=======================================================================================================
DataSet Ds = new DataSet();
SqlConnection Cnx = new SqlConnection();
SqlDataAdapter DA_Livre = new SqlDataAdapter();
SqlDataAdapter DA_Theme = new SqlDataAdapter();

DataRow[] drLivre;
DataRow[] drTheme;

public Form3()
{
InitializeComponent();
}

private void Form3_Load(object sender, EventArgs e)


{
string sCnx = "Data Source=.;Initial Catalog=ADO_DB;Integrated Security=True";
Cnx.ConnectionString = sCnx;

DA_Livre.SelectCommand = new SqlCommand();


DA_Livre.SelectCommand.Connection = Cnx;
DA_Livre.SelectCommand.CommandText = "select * From MD_Livre";

DA_Theme.SelectCommand = new SqlCommand();


DA_Theme.SelectCommand.Connection = Cnx;
DA_Theme.SelectCommand.CommandText = "select * From MD_Theme";

DA_Livre.Fill(Ds, "Livres");
DA_Theme.Fill(Ds, "Themes");

DataColumn[] Cols_L = { Ds.Tables["Livres"].Columns[0] };


Ds.Tables["Livres"].PrimaryKey = Cols_L;

DataColumn[] Cols_Th = { Ds.Tables["Themes"].Columns[0] };


Ds.Tables["Themes"].PrimaryKey = Cols_Th;

DataRelation Rel = new DataRelation("Th_Lv", Ds.Tables["Themes"].Columns[0],


Ds.Tables["Livres"].Columns[4]);

Ds.Relations.Add(Rel);

foreach (DataRow row in Ds.Tables["Livres"].Rows)


{
drLivre = row.GetParentRows("Th_Lv");
for (int i = 0; i < drLivre.Length; i++)
{
comboBox1.Items.Add(drLivre[i][1]);
}
}

foreach (DataRow row in Ds.Tables["Themes"].Rows)


{
drTheme = row.GetChildRows("Th_Lv");
for (int i = 0; i < drTheme.Length; i++)
{
comboBox2.Items.Add(drTheme[i][1]);
}
}
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
DataTable tab = Ds.Tables["Livres"].Clone();

foreach (DataRow drTh in Ds.Tables["Themes"].Rows)


if (drTh[1].ToString() == comboBox1.SelectedItem.ToString())
foreach (DataRow drLv in drTh.GetChildRows("Th_Lv"))
tab.ImportRow(drLv);

dataGridView1.DataSource = tab;
dataGridView1.ClearSelection();
}

private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)


{
foreach (DataRow drL in Ds.Tables["Livres"].Rows)
if (drL[1].ToString() == comboBox2.SelectedItem.ToString())
lblTheme.Text = drL.GetParentRow("Th_Lv")[1].ToString();

Vous aimerez peut-être aussi