我写了下面的代码行
DataTable dataTable = grdServiceList.DataSource as DataTable;
if (dataTable != null)
{
DataView dataView = new DataView(dataTable);
dataView.Sort = e.SortExpression + " " + ConvertSortDirectionToSql(e.SortDirection);
grdServiceList.DataSource = dataView;
grdServiceList.DataBind();
}
public void bind()
{
IEnumerable<_14Muslims.Domain.Entity.Service> Services = context.ServicesFetchWithPaging(ctlPager.PageIndex, ctlPager.PageSize, SearchHint, CountryID, Street, State, City);//.OrderBy(DataGridSortBy);
grdServiceList.DataSource = Services;
grdServiceList.DataBind();
}
在检查debuger模式后,datatable包含空值
答案 0 :(得分:0)
你说datatable contains null
;我相信您的grdServiceList.DataSource
不属于DataTable
类型,因此as
的投射会返回NULL
。我的意思是下面的行
DataTable dataTable = grdServiceList.DataSource as DataTable;
我非常确定,如果您进行明确演员而不是使用像as
这样的DataTable dataTable = (DataTable)grdServiceList.DataSource;
,则会抛出InvalidCastException
。
只是一个猜测,但尝试将其投射到DataSet
并查看是否有效
DataSet ds = grdServiceList.DataSource as DataSet;
if (ds != null)
{
DataView dataView = new DataView(ds.Tables[0]);
dataView.Sort = e.SortExpression + " " + ConvertSortDirectionToSql(e.SortDirection);
grdServiceList.DataSource = dataView;
grdServiceList.DataBind();
}