It looks like you need to have
var DataSource = from data in GridViewData
orderby data.Column1
select data;
Since the orderby clause takes a property name. The clause doesn't parse the string to determine what to order by like Dynamic Queryable does.
Or, in the case of sorting on multiple fields, I would use
var DataSource = from data in GridViewData
select data;
switch(fieldToSortOn)
{
case "Column1":
DataSource = NewDataSource.OrderBy(x => x.Field1);
break;
case "Column2":
DataSource = NewDataSource.OrderBy(x => x.Field2);
break;
...
}
GridView1.DataSource = DataSource;
GridView1.DataBind();
You can also try using
Handle GridView.OnSorting() and create sorting expression dynamically using LINQ[
^].