Monday, 21 July 2014

How to Bind xml to DataGridView Using C#.Net In Windows Application

7/21/2014 - By Pranav Singh 0

This article will explain your how you can bind the read the xml file by using dataset and display into the datagridview using c#.net and windows application.


So for this article first we will create a new windows application. Now we will create a new folder for putting out xml file into this folder. From this folder we will read our xml file. Now for testing purpose put the xml folder into bin-> debug folder.

This xml folder we are putting into debugger folder because whenever we build our project it’s exe created into debug folder.


Here is your XML file.

xml version="1.0" encoding="utf-8" ?>
<CountryDetail>
  <country>
    <Id>1</Id>
    <Name>India</Name>
    <Population>10000200</Population>
  </country>
  <country>
    <Id>2</Id>
    <Name>Pakistan</Name>
    <Population>3002020</Population>
  </country>
  <country>
    <Id>3</Id>
    <Name>America</Name>
    <Population>4343434</Population>
  </country>
  <country>
    <Id>3</Id>
    <Name>Nepal</Name>
    <Population>235676</Population>
  </country>
</CountryDetail>

Now put a data GridView and add the below code into your page.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;

namespace XMLIncombobox
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        /*How to Bind xml to DataGridView Using C#.Net In Windows Application*/
        private void Form1_Load(object sender, EventArgs e)
        {
            BindDropDown();
        }
        ///
        /// Read xml file and bind it to Data GridView
        ///
        public void BindDropDown()
        {
            /*Here for testing put your xml file in debuger folder*/
            /*When you publish the code on that case just create an xml file and addd the
             .xml file into that folder*/
            XmlTextReader xmdatareader = new XmlTextReader("Xml/Country.xml");
            DataSet _objdataset = new DataSet();
            _objdataset.ReadXml(xmdatareader);
            dataGridView1.DataSource = _objdataset.Tables[0];         
        }
    }
}

In above code firs I have read the xml file by using xmltextreader and after that I have  used dataset to read the xml file and storing the data into dataset. Finally I have bind the dataset to dataGridView control.


Now run the application to view the output.




About the Author

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Get Updates

Subscribe to our e-mail newsletter to receive updates.

Share This Post

0 comments:

Please let me know your view

Free Ebooks


About Us

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Contact Us

For writing article in this website please send request by your

GMAIL ID: dotnetpools@gmail.com

Bugs and Suggestions

As we all know that this website is for sharing knowledge and providing proper solution. So while reading the article is you find any bug or if you have any suggestion please mail us at contact@aspdotnet-pools.com.

Partners


Global Classified : Connectseekers.com
© 2014 aspdotnet-pools.com Designed by Bloggertheme9.
back to top