aarona
aarona

Reputation: 37303

How to access an column with special characters using DataTable.Select()?

I have a DataTable with column such as # of Students and would like to sort by this in descending order. Here is my code:

...
dt.Columns.Add(new DataColumn("# of Students", typeof(string)));

// do some stuff... add records etc.

// A runtime error occurs here: "Cannot find column '# of Students'"
var rows = dt.Select("","'# of Students' desc");

// this is just fine.
rows = dt.Select("","# of Students");

How can I access this column if has special characters in its name?

Upvotes: 0

Views: 5094

Answers (2)

ILya
ILya

Reputation: 2778

You can use both [] or `` syntax. Both following snippets are correct:

var rows = dt.Select("","`# of Students` desc");

var rows = dt.Select("","[# of Students] desc");

Upvotes: 3

Antonio Bakula
Antonio Bakula

Reputation: 20693

You should use [] brackets, like this :

var rows = dt.Select("","[# of Students] desc");

Upvotes: 2

Related Questions