Linq数据源, GridView排序(转载)
时间:2010-11-25 来源:steve.z
.aspx.cs文件: public partial class OrderByProperty : System.Web.UI.Page { private GSMDataContext _data; /// /// 构造器。 /// public OrderByProperty() { _data = new GSMDataContext(); } private void bindGridViewData() { if (!IsPostBack) { GridViewDevice.DataKeyNames = new string[] { "ID" }; } string sortExpression = GridViewDevice.Attributes["sortExpression"]; SortDirection sortDirection = GridViewDevice.Attributes["sortDirection"] == "ASC" ? SortDirection.Ascending : SortDirection.Descending; IEnumerable source = _data.Tab_DeviceInfos.Select(info => new Device(info)); if (string.IsNullOrEmpty(sortExpression)) { GridViewDevice.DataSource = source; } else { if (sortDirection == SortDirection.Ascending) { GridViewDevice.DataSource = source.OrderBy(item => item.GetType().GetProperty(sortExpression).GetValue(item, null)); } else { GridViewDevice.DataSource = source.OrderByDescending(item => item.GetType().GetProperty(sortExpression).GetValue(item, null)); } } GridViewDevice.DataBind(); } protected void Page_Load(object sender, EventArgs e) { bindGridViewData(); } protected void GridViewDevice_Sorting(object sender, GridViewSortEventArgs e) { //保存sortExpression和sortDirection。 string sortExpression = e.SortExpression; string sortDirection = "ASC"; if (sortExpression.Equals(GridViewDevice.Attributes["sortExpression"]) && "ASC".Equals(GridViewDevice.Attributes["sortDirection"])) { sortDirection = "DESC"; } GridViewDevice.Attributes.Add("sortExpression", sortExpression); GridViewDevice.Attributes.Add("sortDirection", sortDirection); bindGridViewData(); } } 补充: 文章是昨天写完的,在IEnumerable source = _data.Tab_DeviceInfos.Select(info => new Device(info));这行代码上面,昨天用的是var source = _data.Tab_DeviceInfos.Select(info => new Device(info));。发布了以后,点了下运行,抛了异常,不能根据System.Object类型进行排序。不直接声明为IEnumber的话,Select函数返回的是IQueryable的实例,则会抛出上述的异常。解决方法的话,可以像我这样将其声明为IEnumberable,或者先调用source.AsEnumerable(),又或者用反射获取其类型,然后进行强转。代码如下: 工具类SortUtility.cs: public class SortUtility { public static object Sort(IEnumerable source, string sortExpression, SortDirection sortDirection) { IEnumerator sourceEnumerator = source.GetEnumerator(); if (!sourceEnumerator.MoveNext()) { return null; } Type dataSourceType = source.GetType(); Type dataItemType = typeof(object); if (dataSourceType.HasElementType) { dataItemType = dataSourceType.GetElementType(); } else if (dataSourceType.IsGenericType) { dataItemType = dataSourceType.GetGenericArguments().FirstOrDefault(); } else { if (sourceEnumerator.Current != null) { dataItemType = sourceEnumerator.Current.GetType(); } } // We'll handle things like LINQ to SQL differently by passing the love // on to the provider. PropertyInfo sortProperty = dataItemType.GetProperty(sortExpression); //找不到排序属性,返回原数据。 if (sortProperty == null) { return source; } Type sorterType = typeof(SortUtility<,>).MakeGenericType(dataItemType, sortProperty.PropertyType); object sorterObject = Activator.CreateInstance(sorterType); return sorterType.GetMethod("Sort", new Type[] { dataSourceType, typeof(string), typeof(SortDirection) }) .Invoke(sorterObject, new object[] { source, sortExpression, sortDirection }); } } public class SortUtility { public static IEnumerable Sort(IEnumerable source, string sortExpression, SortDirection sortDirection) { Func sortFunc = item => (ST)typeof(T).GetProperty(sortExpression).GetValue(item, null); if (sortDirection == SortDirection.Ascending) { return source.OrderBy(sortFunc); } else { return source.OrderByDescending(sortFunc); } } } .aspx.cs中绑定数据部分的代码: private void bindGridViewData() { if (!IsPostBack) { GridViewDevice.DataKeyNames = new string[] { "ID" }; } string sortExpression = GridViewDevice.Attributes["sortExpression"]; SortDirection sortDirection = GridViewDevice.Attributes["sortDirection"] == "ASC" ? SortDirection.Ascending : SortDirection.Descending; var source = _data.Tab_DeviceInfos.Select(info => new Device(info)); if (string.IsNullOrEmpty(sortExpression)) { GridViewDevice.DataSource = source; } else { /* if (sortDirection == SortDirection.Ascending) { GridViewDevice.DataSource = source.OrderBy(item => item.GetType().GetProperty(sortExpression).GetValue(item, null)); } else { GridViewDevice.DataSource = source.OrderByDescending(item => item.GetType().GetProperty(sortExpression).GetValue(item, null)); } */ GridViewDevice.DataSource = SortUtility.Sort(source, sortExpression, sortDirection); } GridViewDevice.DataBind(); }
转自:http://www.cnblogs.com/z119977662/archive/2010/11/23/1885263.html