This article will show you how you can save the record in
XML file and read xm l to display in datagridview using c#.net in windows application.
Some of my previous articles are as follows: How
to Add HyperLink and Retrieve Row Value on Link Click in DataGridView #,
Windows application, How
to Add Button Control and Retrieve Row Value on Button Click in DataGridView
Using C#.Net, Windows Application, Splash
Screen With Please Wait OR Loading Message in C#.net in Windows Application,
Save
Panel with Control Inside It as Image C#.net, VB.net in windows application,
Confirmation
Message on Button Click and Capture YES/NO Click by User in Windows
application, C#.
So for this article first we will create a new windows
application and in this application we will add some controls in it. Now we
will add a folder and create an XML file in it.
Your form will look as shown below.
Now on form code we will add the below code.
using System;
using System.Drawing;
using System.Windows.Forms;
using System.Data;
using System.IO;
using System.Xml;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public
Form1()
{
InitializeComponent();
}
public
void GetData()
{
try
{
DataSet
dsResult = new DataSet();
dsResult.ReadXml("StudentXML/StudentXML.xml");
if
(dsResult.Tables.Count != 0)
{
if
(dsResult.Tables[0].Rows.Count > 0)
{
dataGridView1.DataSource = dsResult.Tables[0];
}
}
}
catch
(Exception ex)
{
}
}
private
void Form1_Load(object
sender, EventArgs e)
{
GetData();
}
private
void button1_Click(object
sender, EventArgs e)
{
/*Save*/
using
(DataSet dsResult = new DataSet())
{
dsResult.ReadXml("StudentXML/StudentXML.xml");
if
(dsResult.Tables.Count == 0)
{
StudentModel
objStudentModel = new StudentModel();
objStudentModel.Id = Convert.ToInt32(txtid.Text);
objStudentModel.Name =
txtname.Text;
objStudentModel.Address =
txtaddress.Text;
XmlTextWriter
writer = new XmlTextWriter("StudentXML/StudentXML.xml",
System.Text.Encoding.UTF8);
writer.WriteStartDocument(true);
writer.Formatting = Formatting.Indented;
writer.Indentation = 2;
writer.WriteStartElement("Student");
writer.WriteStartElement("Student");
writer.WriteStartElement("Id");
writer.WriteString(objStudentModel.Id.ToString());
writer.WriteEndElement();
writer.WriteStartElement("Name");
writer.WriteString(objStudentModel.Name);
writer.WriteEndElement();
writer.WriteStartElement("Address");
writer.WriteString(objStudentModel.Address);
writer.WriteEndElement();
writer.WriteEndElement();
writer.WriteEndElement();
writer.WriteEndDocument();
writer.Close();
dsResult.ReadXml("StudentXML/StudentXML.xml");
}
else
{
//--
Add one new row in to dataset and set the column data accordingly.
dsResult.Tables[0].Rows.Add(dsResult.Tables[0].NewRow());
dsResult.Tables[0].Rows[dsResult.Tables[0].Rows.Count - 1]["Id"] = txtid.Text;
dsResult.Tables[0].Rows[dsResult.Tables[0].Rows.Count - 1]["Name"] = txtname.Text.ToUpper();
dsResult.Tables[0].Rows[dsResult.Tables[0].Rows.Count - 1]["Address"] = txtaddress.Text;
dsResult.AcceptChanges();
//--
Write final data to XML file using write method
dsResult.WriteXml("StudentXML/StudentXML.xml", XmlWriteMode.IgnoreSchema);
}
dataGridView1.DataSource =
dsResult.Tables[0];
MessageBox.Show("Data Saved Successfully.");
}
}
private
void button2_Click(object
sender, EventArgs e)
{
this.Close();
}
}
}
|
In above code I have created a function in which I have read
the xml file and bind it to datagridview. Now on button click event I have
first added the stating node and then updated the record and put it in XML
file.
Now we have done run the application to check the output.
Now click on save
xml version="1.0" standalone="yes"?>
<Student>
<Student>
<Id>1</Id>
<Name>Rajesh</Name>
<Address>Address1</Address>
</Student>
<Student>
<Id>2</Id>
<Name>SHYAM</Name>
<Address>Address2</Address>
</Student>
<Student>
<Id>3</Id>
<Name>VINAY</Name>
<Address>Address3</Address>
</Student>
<Student>
<Id>4</Id>
<Name>PRATIMA</Name>
<Address>Address4</Address>
</Student>
</Student>
|
0 comments:
Please let me know your view