Get constant from column ordinal.
-
I need to use a DataTable's column number in a switch statement. I am having problems with the following: const Int32 xxxColumn = (Int32)DataTable.xxxColumn.Ordinal; switch (n) { case xxxColumn: ********; break; } How do I get a constant value from a DataTable column ordinal. Any help will be appreciated, Thanks in advance, Michael
-
I need to use a DataTable's column number in a switch statement. I am having problems with the following: const Int32 xxxColumn = (Int32)DataTable.xxxColumn.Ordinal; switch (n) { case xxxColumn: ********; break; } How do I get a constant value from a DataTable column ordinal. Any help will be appreciated, Thanks in advance, Michael
-
I need to use a DataTable's column number in a switch statement. I am having problems with the following: const Int32 xxxColumn = (Int32)DataTable.xxxColumn.Ordinal; switch (n) { case xxxColumn: ********; break; } How do I get a constant value from a DataTable column ordinal. Any help will be appreciated, Thanks in advance, Michael
See if this gives you useful ideas:
public class SwitchOnDTOrdinal
{
public SwitchOnDTOrdinal(DataTable table)
{
TheTable = table;validColumnNames = table.Columns .Cast() .Select(col => col.ColumnName) .ToList(); } private List validColumnNames; private DataTable TheTable; public void SwitchA(string cname) { if (! validColumnNames.Contains(cname)) { throw new ArgumentException(@"{cname} is not a valid column name"); } switch(TheTable.Columns\[cname\].Ordinal) { case 0: break; case 1: break; case 2: break; case 3: break; default: throw new ArgumentOutOfRangeException(); } }
}
«One day it will have to be officially admitted that what we have christened reality is an even greater illusion than the world of dreams.» Salvador Dali
-
I need to use a DataTable's column number in a switch statement. I am having problems with the following: const Int32 xxxColumn = (Int32)DataTable.xxxColumn.Ordinal; switch (n) { case xxxColumn: ********; break; } How do I get a constant value from a DataTable column ordinal. Any help will be appreciated, Thanks in advance, Michael
Hi, for
Ordinal
I agree with what has already been said. However you might consider working with the columnsName
orCaption
property; if one of those has known values you can still use a simple switch, as C# allows switching on string literals. :)Luc Pattyn [My Articles] Nil Volentibus Arduum