Andreas Solsbacher
Andreas Solsbacher

Reputation: 3

Load .xml to gridview

my problem is that i have to load a .xml file to gridview.

when i click the button to load it in the gridview it shows the datas, but hidden in a +.

in the running program enter image description here

but i want to load it directly to the gridview.

    private void loadbtnxml_Click(object sender, EventArgs e)
    {
        DataSet importierteKonten = new DataSet();
        XmlDocument doc = new XmlDocument();
        doc.Load(@"C:\Users\as\Documents\Visual Studio 2017\Projects\Test2\Test2\bin\Debug\XMLFile1.xml");

        XmlElement exelement = doc.DocumentElement;
        if (exelement != null)
        {
            XmlNodeReader nodereader = new XmlNodeReader(exelement);
            importierteKonten.ReadXml(nodereader, XmlReadMode.Auto);

            bdsKonto.DataSource = importierteKonten;
            bdsKonto.ResetBindings(true);
        }
    }

here is the XML: <?xml version="1.0" encoding="utf-8"?> <Konto> <Konto Kontonummer="1" Kontoname="Peter Zwegat" Alter="55" Kontostand="5462€" /> <Konto Kontonummer="2" Kontoname="Günther Griesgram" Alter="39" Kontostand="2334€" /> <Konto Kontonummer="3" Kontoname="Siegfried Müller" Alter="46" Kontostand="2973€" /> <Konto Kontonummer="4" Kontoname="Ömer Akyol" Alter="20" Kontostand="3518€" /> <Konto Kontonummer="5" Kontoname="Darius Tolkmitt" Alter="23" Kontostand="1947€" /> <Konto Kontonummer="6" Kontoname="Name des Inhabers" Alter="Alter" Kontostand="Kontostand in €" /> <Konto Kontonummer="7" Kontoname="Name des Inhabers" Alter="Alter" Kontostand="Kontostand in €" /> <Konto Kontonummer="8" Kontoname="Name des Inhabers" Alter="Alter" Kontostand="Kontostand in €" /> <Konto Kontonummer="9" Kontoname="Name des Inhabers" Alter="Alter" Kontostand="Kontostand in €" /> </Konto>

Upvotes: 0

Views: 2163

Answers (1)

Chandan Kumar
Chandan Kumar

Reputation: 4638

Here is the solution.

Steps i have mentioned Here

1) I have added a xml file in the application named as "XMLFile1.xml" and reading from that xml file to a string.

2) Convert the xml string to a DataSet >>> DataTable.

3) Assign the DataSource of the DataGridView to the DataTable available inside the DataSet.

Try and let me know if you still face any issues

Code

using System;
using System.Data;
using System.IO;
using System.Windows.Forms;

namespace PopulateGridviewWindowsFormsApp
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void loadbtnxml_Click(object sender, EventArgs e)
        {
            string xmlString = File.ReadAllText(@"D:\My Apps\WinForm Application\PopulateGridviewWindowsFormsApp\PopulateGridviewWindowsFormsApp\XMLFile1.xml");
            DataTable dtKonto = ConvertXmlStringToDatatable(xmlString);
            dataGridView1.DataSource = dtKonto;
        }

        //Convert a xml string to datatable
        public static DataTable ConvertXmlStringToDatatable(string xmlData)
        {
            StringReader sr = new StringReader(xmlData);
            DataSet theDataSet = new DataSet();
            theDataSet.ReadXml(sr);
            return theDataSet.Tables[0];
        }
    }
}

Output

enter image description here

Hope it solves your problem.

Upvotes: 1

Related Questions