-->

22 February 2020

ADO.NET DataView Object to Filter and Sort Rows

  Asp.Net CS By Example       22 February 2020
 ADO.NET DataView Object to Filter and Sort Rows 

 In this post we are learning how to use Filter and Sort methods of DataView class. We can use a DataView object to filter rows previously retrieved into a DataTable. We set the row filter for a DataView object using the RowFilter property. we can also sort the rows in the DataView— we specify the columns to sort using the Sort property.

 Below illustrates how to use Filter and Sort Rows in ADO.NET.

  Code: FrmDataSetWriteXmlFile.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.OracleClient;
namespace ADO_Filter_andSortRows
{
    class Program
    {
        public static void Main()
        {
            string connectionString = "data source=LearnAsp;user id=LearnAsp;"
            +"password=LearnAsp; Pooling=true; Max pool size=200; Min pool size=0;";
            OracleConnection myConn = new OracleConnection(connectionString);

            string selectString = "SELECT deptno,dname, loc   FROM dept  ";

            OracleCommand myCommand = myConn.CreateCommand();

            myCommand.CommandText = selectString;

            OracleDataAdapter myDataAdapter = new OracleDataAdapter();

            myDataAdapter.SelectCommand = myCommand;

            DataSet myDataSet = new DataSet();

            myConn.Open();

            Console.WriteLine("Retrieving a row from the Depart ment  table");
            myDataAdapter.Fill(myDataSet, "dept");

            DataView myDataView =
            new DataView(myDataSet.Tables["dept"]);
            myDataView.RowFilter = "dname = 'IT'";
            // set the Sort property of the DataView object
            myDataView.Sort = "deptno ASC";

            // display the rows in the DataView object
            foreach (DataRowView myDataRowView in myDataView)
            {
                for (int count = 0; count < myDataView.Table.Columns.Count; count++)
                {
                    Console.WriteLine(myDataRowView[count]);
                }
            }

            myConn.Close();
        }
    }
}

  Output:


logoblog

Thanks for reading ADO.NET DataView Object to Filter and Sort Rows

Previous
« Prev Post

No comments:

Post a Comment

Please do not enter any spam link in the comment box.