zoukankan      html  css  js  c++  java
  • DataRow[]、List<DataRow>无法绑定到GridView的问题解决!

     今天一个功能需在内存中实现分页,写下如下代码:

      
     this.gridView1.DataSource = DataTable1.AsEnumerable( ).Skip(( pager.CurrentPageIndex - 1 ) * pager.PageSize ).Take( pager.PageSize );//如第CurrentPageIndex 页获取的数据
     this.gridView1.DataBind( );
      
    抛出异常,找不到“xxx“,xxx是DataTable1的字段,还以为是没有缺少这个字段,其实是DataRow[]、List<DataRow>无法绑定到GridView,找了下资料,有如下方法
      
    IEnumerable<DataRow> rowsOfPage = DataTable1.AsEnumerable( ).Skip(( pager.CurrentPageIndex - 1 ) * pager.PageSize ).Take( pager.PageSize );
    DataTable dtSource = DataTable1.Clone( );
    foreach ( DataRow row in rowsOfPage )
    {
    //dtSource.Rows.Add( row );//throw Exception, row已?经?属?于?另?一?张?表?了?。?
    dtSource.ImportRow( row );//copies row into dtSource
    }
    dtSource.AcceptChanges( );
    this.gridView1.DataSource = dtSource;
    this.gridView1.DataBind( ); 
    找找居然还有更简洁的代码,如下:
     
    this.gridView1.DataSource = DataTable1.AsEnumerable( ).Skip(( pager.CurrentPageIndex - 1 ) * pager.PageSize ).Take( pager.PageSize ).CopyToDataTable( );
    this.gridView1.DataBind( );
     
    反正就是要转换为DataTable,再绑定!

    haha,第一次写“技术”文章,看不懂,没帮助到你,可以批评!如有更好的方法也可分享分享!

  • 相关阅读:
    codeforces A. Chess Placing
    codeforces E. Cyclic Components
    poj1930(小数化分数)
    hdu4497 (正数分解定理+排列组合)
    cf 466 div.2 A. Points on the line
    hdu1576(扩展欧几里得求逆元板子)
    逆元(转载)
    stiring 数..........
    逆元
    矩阵 构造 模板
  • 原文地址:https://www.cnblogs.com/greatalexander/p/1940593.html
Copyright © 2011-2022 走看看