2012-09-19 55 views
0

嗨我傳遞一個數據表的方法「AsJqGridResult」,但「GetPropertyValue」方法返回錯誤,由於「System.Reflection.PropertyInfo propertyInfo」返回NULL。System.Reflection.PropertyInfo propertyInfo返回NULL?

有什麼問題要通過數據表或任何其他問題,請大家幫忙....

控制器

[HttpPost] 
    public ActionResult DynamicGridData(string sidx, string sord, int page, int rows) 
    { 
     var context = GetTable().AsEnumerable(); 
     return (context.AsQueryable().AsJqGridResult(sidx, sord, page, rows)); 
    } 


    public DataTable GetTable() 
    { 
     DataTable table = new DataTable(); 
     table.Columns.Add("Number", typeof(int)); 
     table.Columns.Add("Abr", typeof(string)); 
     table.Columns.Add("Country", typeof(string)); 
     table.Columns.Add("Date", typeof(DateTime)); 

     table.Rows.Add(1, "AF", "AFGHANISTAN", DateTime.Now); 
     table.Rows.Add(2, "AX", "ÅLAND ISLANDS", DateTime.Now); 

     return table; 
    } 

helper方法

public static JsonResult AsJqGridResult<T>(this IQueryable<T> source, string column, string sortOrder, int page, int pageSize) 
{ 
    int pageIndex = Convert.ToInt32(page) - 1; 
    int totalRecords = source.Count(); 
    int totalPages = (int) Math.Ceiling((float) totalRecords/(float) pageSize); 

    var list = (sortOrder.ToLower()=="asc") ? 
     source.OrderBy(new Func<T, IComparable>(item => (IComparable) GetPropertyValue(item, column))).Skip(pageIndex*pageSize).Take(pageSize) : 
     source.OrderByDescending(new Func<T, IComparable>(item => (IComparable) GetPropertyValue(item, column))).Skip(pageIndex*pageSize).Take(pageSize); 


    var jsonData = new 
    { 
    total = totalPages, 
    page, 
    records = totalRecords, 
    rows = (
      from item in list 
      select new 
         { 
         i = Guid.NewGuid(), 
         cell = GetGridCells(item) 
         }).ToArray() 
    }; 

    return new JsonResult {Data = jsonData}; 
} 

/// <summary> 
/// Gets the property value. 
/// </summary> 
/// <param name="obj">The obj.</param> 
/// <param name="property">The property.</param> 
/// <returns></returns> 
private static object GetPropertyValue(object obj, string property) { 
    System.Reflection.PropertyInfo propertyInfo = obj.GetType().GetProperty(property); 
    return propertyInfo.GetValue(obj, null); 
} 
+0

http://blog.slaks.net/2011/01/binding-to-lists-of-datarows.html – SLaks

回答

0

做數據表沒有屬性的列。

相反,你需要調用TypeDescriptor.GetProperties()在桌子上,這將使用ITypedList實施得到PropertyDescriptor S爲實際列。

+0

感謝您的回覆,但我沒有得到,請你拿出一些代碼..日Thnx – user584018