Bonjour tout le monde,

Voilà je début en programmation et je me suis lancé dans la création d'un petit programme de contacts. Trois textBox Nom, Prénom et E-mail avec trois boutons Ajouter, Supprimer et Modifier et un dataGridView. Cette partie du programme fonctionne. Mais ça coince pour la seconde partie importation et exportation XML Deux boutons Importer et Exporter avec un second dataGridView pour visualiser. Lors de l'importation et de l'exportation j'ai ce message d'erreur: Impossible de sérialiser la DataTable. Le nom du DataTable n'est pas défini.

Voici un screen de lu programme :

Nom : Contact.png
Affichages : 1906
Taille : 9,0 Ko

et le code source :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
 
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.Xml;
using System.Xml.Serialization;
using System.Xml.Schema;
 
namespace Fiche_client
{
    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)
        {
            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();
        }
 
 
        // Importer
        private void button1_Click_1(object sender, EventArgs e)
        {
            try
            {
                DataTable dtb = new DataTable();
                dtb.WriteXml(@"F:\test2.xml");
                dataGridView2.DataSource = dtb;
                MessageBox.Show("Données importées.");
            }
 
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
 
        // Exporter
        private void button2_Click(object sender, EventArgs e)
        {
            try
            { 
                dt.WriteXml(@"F:\test2.xml", XmlWriteMode.WriteSchema);
                MessageBox.Show("Données exportées.");
            }
 
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
 
 
 
    }
}