IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Windows Forms Discussion :

C# dataGridView Import Export XML


Sujet :

Windows Forms

  1. #1
    Candidat au Club
    Homme Profil pro
    Technicien
    Inscrit en
    Janvier 2016
    Messages
    4
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Belgique

    Informations professionnelles :
    Activité : Technicien

    Informations forums :
    Inscription : Janvier 2016
    Messages : 4
    Points : 2
    Points
    2
    Par défaut C# dataGridView Import Export XML
    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 : 1824
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);
                }
            }
     
     
     
        }
    }

  2. #2
    Membre averti
    Homme Profil pro
    Développeur .NET
    Inscrit en
    Janvier 2008
    Messages
    233
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 42
    Localisation : France, Rhône (Rhône Alpes)

    Informations professionnelles :
    Activité : Développeur .NET

    Informations forums :
    Inscription : Janvier 2008
    Messages : 233
    Points : 336
    Points
    336
    Par défaut
    Avec la balise "CODE", c'est mieux :
    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
    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);
                }
            }
        }
    }
    "Hope for the best, but prepare for the worst."

  3. #3
    Candidat au Club
    Homme Profil pro
    Technicien
    Inscrit en
    Janvier 2016
    Messages
    4
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Belgique

    Informations professionnelles :
    Activité : Technicien

    Informations forums :
    Inscription : Janvier 2016
    Messages : 4
    Points : 2
    Points
    2
    Par défaut
    Merci Casiii c'est mieux.

    Sinon personne n'a une idée pour résoudre ce problème?

  4. #4
    Candidat au Club
    Homme Profil pro
    Technicien
    Inscrit en
    Janvier 2016
    Messages
    4
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Belgique

    Informations professionnelles :
    Activité : Technicien

    Informations forums :
    Inscription : Janvier 2016
    Messages : 4
    Points : 2
    Points
    2
    Par défaut
    Je viens de tester XmlDataDocument, mais Visual Studio me dit qu'il est obsolète.
    Nom : XmlDocument.png
Affichages : 1655
Taille : 8,4 Ko

    Je vais continuer à chercher avec WriteXml. Je vous tiens au courant.

  5. #5
    Candidat au Club
    Homme Profil pro
    Technicien
    Inscrit en
    Janvier 2016
    Messages
    4
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Belgique

    Informations professionnelles :
    Activité : Technicien

    Informations forums :
    Inscription : Janvier 2016
    Messages : 4
    Points : 2
    Points
    2
    Par défaut Solution
    J'ai trouvé la solution, reste a peaufiner un peu, mais cela fonctionne.

    Voici 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
    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);
                }
            }
        }
    }

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Format générique d'import/export xml
    Par LEK dans le forum Accès aux données
    Réponses: 0
    Dernier message: 06/08/2012, 12h57
  2. import export xml
    Par robbie_slash dans le forum Oracle
    Réponses: 2
    Dernier message: 19/10/2010, 10h16
  3. [Mapping/Import/Export] XML et SGBDR ?
    Par Eric Lavanda dans le forum Décisions SGBD
    Réponses: 3
    Dernier message: 18/01/2006, 12h31
  4. [10g] Outil import/export DB/CSV, XML => BPEL ?
    Par lex0072 dans le forum Oracle
    Réponses: 2
    Dernier message: 28/09/2005, 11h38

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo