Sorting with DataGridView using C#.

Introduction

In this article I m going to discuss how to use DataGridView in window form and how to bind data from the XML file.

Here I am using the following XML file. This file contains the record of some books.

<?xml version="1.0" encoding="utf-8" ?>
<bookstore>
    <book>
        <SrNumber>1</SrNumber>
        <Title>ASP.NET 2.0</Title>
        <Price>100 </Price>
        <Author>Puru </Author>
        <PublicationYear>1981 </PublicationYear>
    </book>
    <book>
        <SrNumber>2</SrNumber>
        <Title>Action Front </Title>
        <Price>110 </Price>
        <Author>Boyd Cable </Author>
        <PublicationYear>1989 </PublicationYear>
    </book>
    <book>
        <SrNumber>3</SrNumber>
        <Title>Abraham Lincoln </Title>
        <Price>180 </Price>
        <Author>James Russell Lowell </Author>
        <PublicationYear>1992 </PublicationYear>
    </book>
    <book>
        <SrNumber>4</SrNumber>
        <Title>Wisteria Lodge, The </Title>
        <Price>150 </Price>
        <Author>Arthur Conan Doyle </Author>
        <PublicationYear>1983 </PublicationYear>
    </book>
    <book>
        <SrNumber>5</SrNumber>
        <Title>Club of Queer Trades, The </Title>
        <Price>175 </Price>
        <Author>Lowell </Author>
        <PublicationYear>2002 </PublicationYear>
    </book>
    <book>
        <SrNumber>6</SrNumber>
        <Title>Anthem </Title>
        <Price>250 </Price>
        <Author>Ayn Rand </Author>
        <PublicationYear>1971 </PublicationYear>
    </book>
    <book>
        <SrNumber>7</SrNumber>
        <Title>Common Sense </Title>
        <Price>250 </Price>
        <Author>Thomas Paine </Author>
        <PublicationYear>1969 </PublicationYear>
    </book>
    <book>
        <SrNumber>8</SrNumber>
        <Title>Damaged Goods </Title>
        <Price>300 </Price>
        <Author>Upton Sinclair </Author>
        <PublicationYear>2000 </PublicationYear>
    </book>
    <book>
        <SrNumber>9</SrNumber>
        <Title>Earth to the Moon </Title>
        <Price>50 </Price>
        <Author>Jules Verne </Author>
        <PublicationYear>1992 </PublicationYear>
    </book>
    <book>
        <SrNumber>10</SrNumber>
        <Title>Hard Times </Title>
        <Price>90 </Price>
        <Author>Charles Dickens </Author>
        <PublicationYear>2003 </PublicationYear>
    </book>
</bookstore>

Now lets start how to use DataGridView. Simply open Visual Studio clik on File -> New -> Project and drag a DataGridView control from the toolbar on the form like as follows.

img1.JPG

Figure 1. DataGridView control on Window Form.

I also put four buttons to perform sorting by various types. Now I m writing C# code to binding the data and perform the sorting. On the form load dataGridView is binding. And on button click you can see the code of sorting.

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

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

        private void Form1_Load(object sender, EventArgs e)
        {
            XmlDataDocument xmlDatadoc = new XmlDataDocument();
            xmlDatadoc.DataSet.ReadXml("C:\\books.xml");

            DataSet ds = new DataSet("Books DataSet");
            ds = xmlDatadoc.DataSet;

            dataGridView1.DataSource = ds.DefaultViewManager;
            dataGridView1.DataMember = "Book";
        }

        private void button1_Click(object sender, EventArgs e)
        {
            XmlDataDocument xmlDatadoc = new XmlDataDocument();
            xmlDatadoc.DataSet.ReadXml("C:\\books.xml");

            DataSet ds = new DataSet("Books DataSet");
            ds = xmlDatadoc.DataSet;

            DataView myDataView = ds.Tables[0].DefaultView;
            myDataView.Sort = "title ASC";

            dataGridView1.DataSource = myDataView;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            XmlDataDocument xmlDatadoc = new XmlDataDocument();
            xmlDatadoc.DataSet.ReadXml("C:\\books.xml");

            DataSet ds = new DataSet("Books DataSet");
            ds = xmlDatadoc.DataSet;

            DataView myDataView = ds.Tables[0].DefaultView;
            myDataView.Sort = "PublicationYear ASC";

            dataGridView1.DataSource = myDataView;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            XmlDataDocument xmlDatadoc = new XmlDataDocument();
            xmlDatadoc.DataSet.ReadXml("C:\\books.xml");

            DataSet ds = new DataSet("Books DataSet");
            ds = xmlDatadoc.DataSet;

            DataView myDataView = ds.Tables[0].DefaultView;
            myDataView.Sort = "Price ASC";

            dataGridView1.DataSource = myDataView;
        }

        private void button4_Click(object sender, EventArgs e)
        {
            XmlDataDocument xmlDatadoc = new XmlDataDocument();
            xmlDatadoc.DataSet.ReadXml("C:\\books.xml");

            DataSet ds = new DataSet("Books DataSet");
            ds = xmlDatadoc.DataSet;

            DataView myDataView = ds.Tables[0].DefaultView;
            myDataView.Sort = "Author ASC";

            dataGridView1.DataSource = myDataView;
        }
    }
}

Output

Now debug the application and you will get the following output.

img2.JPG

Figure 2. Record in DataGridView.

Sorting by Author

If you want to sort the record by author then click on 'Sort By Author' button.

Sort by author.JPG

Figure 3. Record sort by authors

Sorting by Title

If you want to sort the record by title then click on 'Sort by Title' button.

Sort by title.JPG

Figure 4. Record sort by Title

Sorting by Year

If you want to sort the record by year then click on 'Sort by Publication Year' button.

Sort by year.JPG

Figure 5. Record sort by publication year.


Similar Articles