How to use DataTable.Select() when the DataColumn.ColumnName contains a space?

I have a DataColumn that contains an space and I just couldn’t get the DataTable.Select(“Column Name=value) function to work.

So i found a solution on some other guys wordpress blog here.

The answer should be obvious to those who use SQL. In SQL to use a space, it often adds square brackets around the column names. [Column Name]. Yes, using square brackets is the solution.

String colName = "Column Name";
String Value = "some data";
DataTable.Select("[" + colName + "]='" + value + "'");

4 Comments

  1. ramz says:

    can anyone help me how to fix this… i should get records because id is greater than 1

    Dim d As New DataSet2

    Dim table As DataTable = d.Tables(“ClientsPersonal”)

    ‘ Presuming the DataTable has a column named Date.
    Dim expression As String
    expression = “id > 1”
    Dim foundRows() As DataRow

    ‘ Use the Select method to find all rows matching the filter.
    foundRows = table.Select(expression)
    MsgBox(foundRows.Count)
    Dim i As Integer
    ‘ Print column 0 of each returned row.
    For i = 0 To foundRows.GetUpperBound(0)
    MsgBox(foundRows(i)(0))
    Next i

  2. Gulab says:

    Thanks very much. Helped me.

Leave a Reply