Skip to main content

Filtering Records in a Data Table in ADO.NET

The below code snippet shows how to filter data in a data table based on certain values.
       
///
        /// Filters the Data Table based on the Employee ID and returns the No. of          
         Row Count available.
        ///
        ///

        ///

        private void button1_Click(object sender, EventArgs e)
        {

            DataTable dtTestTable = new DataTable();
            DataRow drRow;

            //Validating the Table
            if (dtTestTable != null & dtTestTable.Columns.Count > 0)
            {
                dtTestTable.Rows.Clear();
                dtTestTable.Columns.Clear();
            }

            //creating columns
            dtTestTable.Columns.Add("EmployeeID", typeof(System.Int16));
            dtTestTable.Columns.Add("FirstName", typeof(System.String));
            dtTestTable.Columns.Add("LastName", typeof(System.String));

            //Adding Rows
            for (int rowCount = 1; rowCount <= 10; rowCount++)
            {
                drRow = dtTestTable.NewRow();
                drRow["EmployeeID"] = rowCount;
                drRow["FirstName"] = "First Name of Row " + rowCount;
                drRow["LastName"] = "First Name of Row " + rowCount;
                dtTestTable.Rows.Add(drRow);

            }
            //display Total Records Count message
            MessageBox.Show(dtTestTable.Rows.Count.ToString ());

            //filter data table rows based on the employee ID;
            Int16  empID=9;

            //Set the filter condition and assign to RowFilter
            dtTestTable.DefaultView.RowFilter = "EmployeeID=" + empID;

            //display the filtered record count
            MessageBox.Show(dtTestTable.DefaultView.ToTable().Rows.Count.ToString ());

        }

Happy Learning !!!

Comments

Popular posts from this blog

Difference between Visual Studio 2017 Editions – Community, Professional and Enterprise

When Microsoft introduced full featured community Edition for developers, I was curious to know what all differences it has with other paid versions? Because I can almost do everything with the community edition then why do I need a paid version? When I searched MSDN I found my answer and difference was quite considerable. However, if you are an Individual Developer and doesn’t need too many features, then community edition is best to start with. There are notable differences between each of the versions/editions and it’s worth knowing it. Instead of I type everything here, Microsoft made it very easy for us to compare the various editions and take a decision. Compare Visual Studio Editions https://www.visualstudio.com/vs/compare/ Microsoft nicely explains each the notable difference between various editions – don’t miss to go through this. Happy Learning !!! Harsh Shah

Azure Strategy and Implementation Guide - Download Free ebook

Are you in the process of cloud migration or just started your journey towards Azure ? If yes then this is the right time for you to go through the book which Microsoft just released it and it's available for Download for Free. This book is called Azure Strategy and Implementation Guide. Download it from here:  https://azure.microsoft.com/en-us/resources/azure-strategy-and-implementation-guide/en-us/ Happy Learning !!!

Download Power Commands for Visual Studio 2010

PowerCommands 10.0 is a set of useful extensions for the Visual Studio 2010 adding additional functionality to various areas of the IDE. Download:  http://visualstudiogallery.msdn.microsoft.com/e5f41ad9-4edc-4912-bca3-91147db95b99 Below is a list of the commands included in PowerCommands for Visual Studio 2010 version 10.0. Enable/Disable PowerCommands in Options dialog This feature allows you to select which commands to enable in the Visual Studio IDE. Point to the Tools menu, then click Options. Expand the PowerCommands options, then click Commands. Check the commands you would like to enable. Note: All power commands are initially defaulted Enabled. Format document on save / Remove and Sort Usings on save The Format document on save option formats the tabs, spaces, and so on of the document being saved. It is equivalent to pointing to the Edit menu, clicking Advanced, and then clicking Format Document. The Remove and sort usings option removes unused using state...