Filtering Datatable with Select Conditions and Sorting in C#.NET


We can filter data in Datatable using Datatable.Select method. We can also sort the filtered data.
We can also apply AND , OR conditions while filtering and sorting. 


Sample Datatable

private DataTable FillDatatoGrid()
        {
            DataTable dt = new DataTable("Emp");

            //Adding columns to table Emp
            DataColumn colEmpid = new DataColumn("Empid", typeof(System.Int32));
            DataColumn colName = new DataColumn("Name", typeof(System.String));
            DataColumn colDept = new DataColumn("Department", typeof(System.String));

            //Adding columns to datatable
            dt.Columns.AddRange(new DataColumn[] { colEmpid, colName, colDept });

            //Adding data
            dt.Rows.Add(1000, "John Smith Brown", "Finance");
            dt.Rows.Add(1001, "Carry Brown", "Engineering");
            dt.Rows.Add(1002, "Candle Pencil", "Marketing");
            dt.Rows.Add(1003, "Graham Bell", "Engineering");
            dt.Rows.Add(1004, "Peter Kevin", "Finance & Engineering");

            return dt;
        }


Example program to Filtering Data using Datatable.Select Method , AND, OR Conditions and Sorting



//Fill data into datatable. FillDatatoGrid method is defined above
        DataTable dataTable=FillDatatoGrid();

         //using Datatable.Select Method
        DataRow[] rowsFiltered = dataTable.Select("Empid=1000");

        DataRow[] rowsFilteredSorting = dataTable.Select("Empid=1000","Empid desc" );

         //Filtering datatable with OR Conditoin and Sorting
        DataRow[] rowsFilteredConditionOR = dataTable.Select("Empid=1000 OR Empid=1001", "Empid desc");

        //Filtering datatable with AND Conditoin
         //Strings should be enclosed with single quotes
        DataRow[] rowsFilteredConditionAND = dataTable.Select("Empid=1001 AND Name='Carry Brown'", "Empid desc");

Please note, strings should be enclosed with single quotes while filtering data using Datatable.Select method.

Zooming in Winforms Webbrowser Control in .NET
Hiding a property from displaying in Datagridview in C#.NET
Logic to detect if Application is started from Citrix/Terminal Server/PC in C#
Winforms Application.DoEvents Method
Modify XML file with tagname in c#.net
Read Machine IP Address in C#
Check if IIS is running using C#
Implementing IEnumerable in C#
Built in Delegates in .NET Framework
Changing Winform webbrowser control IE version in C#.NET
Set the dropdown width of any combobox based on the data
Difference between Const and ReadOnly
Example for Property Change Notification in C#.NET
Example Program using Delegates and Events in C#
AutoComplete in .NET Winforms TextBox
How to read data from XML String and insert in to table in SQL Server
WebBrowser.navigate Data Submission by Get or Post Methods
Datagridview Paging
Copy Datagridview cell value using ContextMenu
Inserting data in Datatable in C#
Cookie in .NET
Using Control's Child Index and Dock property for achieving desired layout in a form.
Accessing section group inside configuration of web.config
Create File in .NET C#
Change button shape in .net
Dispose vs Finalize in .net




Datatable.Select AND Condition, Datatable.Select OR Condition, Datatable.Select sorting in C#, Filter data in datatable, Filter and Sort data in Datatable in .NET