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
|
using System;
using System.Data;
using System.Windows.Forms;
using System.Xml;
namespace Fiche_Contact
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
// Création du tableau
DataTable dt = new DataTable();
int i, j;
// Création des collones du dataGridView1
private void Form1_Load(object sender, EventArgs e)
{
dt.Columns.Add("Nom", typeof(string));
dt.Columns.Add("Prénom", typeof(string));
dt.Columns.Add("E-mail", typeof(string));
}
// Ajouter
private void button3_Click(object sender, EventArgs e)
{
try
{
dt.Rows.Add(textBox1.Text, textBox2.Text, textBox3.Text);
dataGridView1.DataSource = dt;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
// Supprimer
private void button4_Click(object sender, EventArgs e)
{
try
{
dt.Rows.RemoveAt(dataGridView1.CurrentCell.RowIndex);
dataGridView1.DataSource = dt;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
// Modifier
private void button5_Click(object sender, EventArgs e)
{
DataGridViewRow row = dataGridView1.Rows[i];
row.Cells[0].Value = textBox1.Text;
row.Cells[1].Value = textBox2.Text;
row.Cells[2].Value = textBox3.Text;
}
// Selectionne une ligne du datagridview
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
try
{
i = e.RowIndex;
DataGridViewRow row = dataGridView1.Rows[i];
textBox1.Text = row.Cells[0].Value.ToString();
textBox2.Text = row.Cells[1].Value.ToString();
textBox3.Text = row.Cells[2].Value.ToString();
}
catch(Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
// Importer
private void button1_Click_1(object sender, EventArgs e)
{
try
{
XmlReader xmlFile;
xmlFile = XmlReader.Create("Contacts.xml", new XmlReaderSettings());
DataSet ds = new DataSet();
ds.ReadXml(xmlFile);
dataGridView2.DataSource = ds.Tables[0];
MessageBox.Show("Fichier Impoter");
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
// Exporter
private void button2_Click(object sender, EventArgs e)
{
try
{
DataSet ds = new DataSet();
ds.Tables.Add(dt);
ds.WriteXml("Contacts.xml");
MessageBox.Show("Fichier Exporter");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
} |
Partager