Selecting and Sorting in DataTable – ASP.net C#

Author posted by Jitendra on Posted on under category Categories ASP.NET and tagged as Tags with Leave a comment on Selecting and Sorting in DataTable – ASP.net C#

Demonstrates that how to select and sort the Datatable in ASP.Net , C#

DataTable in C# have very reach features to store the data in Tabular forms and also to display sorted or selected data on predefined category. Below i have provided the source code which demonstrates that how to select and sort the Datatable in ASP.Net.

Sorting and Selecting DataTable in ASP

below is the snap of C# File:

 public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            DataTable dt = prepareTable();
            GridView1.DataSource = dt;
            GridView1.DataBind();

            dt.DefaultView.Sort = "DOB ASC";
            GridView2.DataSource = dt;
            GridView2.DataBind();

            dt.DefaultView.Sort = "Age DESC";
            GridView3.DataSource = dt;
            GridView3.DataBind();

            dt.DefaultView.Sort = "Id DESC, Age ASC";
            GridView4.DataSource = dt;
            GridView4.DataBind();

            DataRow[] dtRows = dt.Select("Age > 20", "Age ASC");
            DataTable dt1 = new DataTable();
            dt1.Columns.Add("Id", typeof(int));
            dt1.Columns.Add("Name", typeof(string));
            dt1.Columns.Add("Age", typeof(int));
            dt1.Columns.Add("DOB", typeof(DateTime));

            foreach (DataRow dr in dtRows)
            {
                dt1.Rows.Add(dr[0], dr[1], dr[2],dr[3]);
            }

            GridView5.DataSource = dt1;
            GridView5.DataBind();
        }

        /// <summary>
        /// Prepares the table for Data.
        /// </summary>
        /// <returns></returns>
        private DataTable prepareTable()
        {
            DataTable rs = new DataTable();
            rs.Columns.Add("Id", typeof(int));
            rs.Columns.Add("Name", typeof(string));
            rs.Columns.Add("Age", typeof(int));
            rs.Columns.Add("DOB", typeof(DateTime));

            AddItems(rs,1,"Tuiya",24,DateTime.Now.AddYears(-20));
            AddItems(rs, 2, "Sonu", 20, DateTime.Now.AddYears(-21));
            AddItems(rs, 4, "Rohan", 19, DateTime.Now.AddYears(-22));
            AddItems(rs, 4, "Manoranjan", 27, DateTime.Now.AddYears(-23));
            AddItems(rs, 1, "Santosh", 24, DateTime.Now.AddYears(-20));

            return rs;
        }

        /// <summary>
        /// Adds the items.
        /// </summary>
        /// <param name="rs">The DataTable.</param>
        /// <param name="id">The id.</param>
        /// <param name="name">The name.</param>
        /// <param name="age">The age.</param>
        /// <param name="dob">The dob.</param>
        private static void AddItems(DataTable rs,int id,string name,int age,DateTime dob)
        {
            DataRow dtRow = rs.NewRow();
            dtRow[0] = id;
            dtRow[1] = name;
            dtRow[2] = age;
            dtRow[3] = dob;
            rs.Rows.Add(dtRow);
        }
    }

Working Demo of DataTable

Related posts

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.