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.
Some of my previous articles are as follows: Read
XML File into DataSet and Populate in ComboBox Using C# in 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.
0 comments:
Please let me know your view