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
|
public partial class Form2 : Form
{
private BindingSource bds = new BindingSource();
private DataSet ds = new DataSet("PneusBennett");
public Form2()
{
InitializeComponent();
}
private void btnAdd_Click(object sender, EventArgs e)
{
bds.AddNew();
}
//les 2 buttons Add et Del sont inutiles car la synchronisation est
// assure entre le datable et les controls lies par BindingSource
// button inutile à virer-saisie dans le dgv ou les controls suffit !!!
private void btnEdit_Click(object sender, EventArgs e)
{
}
// button inutile à virer-suppression de la ligne du dgv (touche supp) !!!
private void btnDel_Click(object sender, EventArgs e)
{
}
private void btnLoad_Click(object sender, EventArgs e)
{
ds.ReadXml("Data1.xml");
DataTable dt = ds.Tables[0];
bds.DataSource = dt;
dataGridView1.DataSource = bds;
tbNom.DataBindings.Clear();
tbPrenom.DataBindings.Clear();
tbAdresse.DataBindings.Clear();
tbAdresse.DataBindings.Clear();
tbNom.DataBindings.Add("Text", bds, dt.Columns[0].ColumnName, true, DataSourceUpdateMode.OnPropertyChanged);
tbPrenom.DataBindings.Add("Text", dt, dt.Columns[1].ColumnName, true, DataSourceUpdateMode.OnPropertyChanged);
tbAdresse.DataBindings.Add("Text", dt, dt.Columns[2].ColumnName, true, DataSourceUpdateMode.OnPropertyChanged);
tbCodePostal.DataBindings.Add("Text", dt, dt.Columns[3].ColumnName, true, DataSourceUpdateMode.OnPropertyChanged);
}
private void btnSave_Click(object sender, EventArgs e)
{
DataTable dt = ds.Tables[0];
ds.WriteXml("Data1.xml");
}
private void textBoxSearch_KeyDown_1(object sender, KeyEventArgs e)
{
if (e.KeyCode != Keys.Enter) return;
string search = null;
DataView view = ds.Tables[0].DefaultView;
if (!String.IsNullOrEmpty(textBoxSearch.Text))
{
search = " Nom = '" + textBoxSearch.Text + "'";
}
view.RowFilter = search;
dataGridView1.Focus();
}
private void button1_Click(object sender, EventArgs e)
{
for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)
{
if (dataGridView1.Rows[i].Cells[1].Value.ToString() != textBoxSearch.Text)
dataGridView1.Rows[i].Visible = false;
}
} |
Partager