DataTables are used to represent the data in a tabular format with several rows, columns, and constraints. There are several methods provided by the DataTable class for our easiness. Out of them, we will be talking about the Select() in this article. Select method is used to get the array of data of DataRow objects.

To better understand this tutorial, you should know the following C# programming topics:

DataTable is provided by the System.Data namespace and it has several overloaded methods:

  1. Select()
  2. Select(String)
  3. Select(String, String)
  4. Select(String, String, DataViewRowState)

From the above list, we use the Select(String) method to get the array of data. The Select method takes one String argument, which is the condition in our case. After obtaining the DataRow array, we can iterate through it and get the values printed. Select methods give the feeling like we are querying a database, which is not.

Filtering Datatable in C#

static void Main(string[] args) {
      //create a datatable
      DataTable empTable = new DataTable();

      //create new columns
      DataColumn empName = new DataColumn();
      //set the data type of the column
      empName.DataType = System.Type.GetType("System.String");
      //set column name
      empName.ColumnName = "Name";

      DataColumn empAge = new DataColumn();
      empAge.DataType = System.Type.GetType("System.Decimal");
      empAge.ColumnName = "Age";

      DataColumn empSalary = new DataColumn();
      empSalary.DataType = System.Type.GetType("System.Decimal");
      empSalary.ColumnName = "Salary";
        
      //add the created columns to the datatable
      empTable.Columns.Add(empName);
      empTable.Columns.Add(empAge);
      empTable.Columns.Add(empSalary);

      //insert data to the datatable
      empTable.Rows.Add("John",23, 10000);
      empTable.Rows.Add("Mark",45, 25000);
      empTable.Rows.Add("Simon",27, 45000);
      empTable.Rows.Add("Mel",56, 21000);
      empTable.Rows.Add("Wien",34, 1000);

      //use the select method
      DataRow[] resultRow = empTable.Select("Age > 30 AND Salary > 10000");
      foreach (DataRow newRow in resultRow)
      {
          Console.WriteLine("Name: {0} Age: {1} Salary: {2}", newRow[0], 
                                  newRow[1], newRow[2]);
      }
      Console.ReadLine();
}

Output

Name: Mark Age: 45 Salary: 25000
Name: Mel Age: 56 Salary: 21000

Code explanation

First, we create a dataTable object, and then we create the required columns that should be included in our dataTable. For this, we should use the DataColumn class. After creating the DataColumn object, we can specify its data type and column name as its properties. In our example, we have created three columns namely “Name,” “Age,” and “Salary.” Next, we should add the created columns to the dataTable object that we created using the Columns.Add() method. As we use a dataTable in this example, we need to insert the data into the dataTable object. For that we use Rows.Add() with the row values. Add() method takes the exact number of parameters equivalent to the columns count we defined earlier. Less or more parameters to the Add() method will cause a compile-time error.

Finally, we can use the Select method with the condition as the parameter to query the dataTable. After obtaining the result set, we iterate through it and print the required fields as shown below.

Recommended Reading

Last modified: March 21, 2019