I am having an error using the following code,

DataView dtView = new DataView(dt);
columname = "ItemNo";
Cond procedure = Cond.checknum; //I had a public enum Cond { checknum, checkspace }
procedure.Equals(null); //Null because I am changing the procedure below in the program           
List<string> Mapcol = new List<string>(); 
foreach (string cols in dt.Columns)
{ Mapcol.Add(cols); } 
dtView.Sort = Mapcol.Contains(columname)+ " ASC";

I am retriving data from a .csv and is already at the DataTable called dt... But I am unable to pass the columns to the ColumnMap list...
Error = Unable to cast object of type 'System.Data.DataColumn' to type 'System.String'.

Recommended Answers

All 3 Replies

dt.Columns returns an System.Data.DataColumn IEnumerable, but you are trying to force it to be a string (line 6). Use foreach (Column cols in dt.Columns) instead

dt.Columns returns an System.Data.DataColumn IEnumerable, but you are trying to force it to be a string (line 6). Use foreach (Column cols in dt.Columns) instead

I tried that but, "The type namespace 'Column' could not be found..." What I am missing?

Sorry, the proper name is DataColumn

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.