Thursday, September 24, 2020

Sorting and Filtering ADO Objects

Listed below is an example of using Sorting and Filtering on ADO objects.  This examples utilizes an Oracle DB, but ADO objects can be used with various databases.  In this situation, the entire data table is retrieved in memory.  When needed, that data table can be sorted using a specific column or filtered using the SELECT method with the specified predicate.

 

        public void ProcesssData()

        {

//Retrieve entire data table into memory

gPOLines = RGNdm.GetLineNumsByPO(PO);


 //Create a VIEW from the in-memory data table

var newView = gPOLines.DefaultView.ToTable(false, "PART_ID", "PO_NUMBER", "CREATE_DATE", "VENDOR_NAME", "INITIATED_BY");


//SORT by desired field

gPOLines.DefaultView.Sort = " PART_ID ASC ";

 

//SELECT (filter) for specific criteria

                DataRow[] filteredRows = gPOLines.Select($" PART_ID = '{cbPartID1.Text}' ");

 

                if (filteredRows.Count() >= 1)

                {

                    decimal userOrderQty = (decimal)filteredRows[0].Field<double>("USER_ORDER_QTY");

                }

        }

 

        public DataTable GetLineNumsByPO(string PO)

        {

            string sql = $" SELECT LINE_NO, PART_ID, USER_ORDER_QTY FROM PO_TABLE ";

            OracleDataAdapter da = new OracleDataAdapter(sql, User.connString);

            DataTable tbl = new DataTable();

            da.Fill(tbl);

            return tbl;

        }

 

No comments:

Post a Comment