• 绑定数组对象DataTable.Select返回值DataRow[]


     

    方法一:

    DataTable dt = (DataTable)gvDraftList.DataSource;
    DataSet ds=new DataSet();
    ds.Merge(dt.Select(where));

    gvDraftList.DataSource = ds.Tables[0];

    方法二:

    DataTable dt = (DataTable)gvDraftList.DataSource;
    gvDraftList.DataSource = new DataView(dt, where, "", DataViewRowState.CurrentRows).ToTable();

    方法三:

    DataTable dt = (DataTable)gvDraftList.DataSource;

    DataTable temp=dt.Clone();
    foreach (DataRow dr in dt.Select(where))
    {
          //temp.Rows.Add(dr); //出错提示为:该行已经属于另一个表
          temp.Rows.Add(dr.ItemArray);
    }
    gvDraftList.DataSource = temp;

    使用扩展方法:

    using System.Data;

    namespace Bll.Ext.Object
    {
        public static class ObjExt
        {
            public static DataTable Select(this object o, string where)
            {
                DataTable dt=o as DataTable;
                if (dt !=null)
                {
                    return new DataView(dt, where, "", DataViewRowState.CurrentRows).ToTable();
                }
                else
                {
                    return null;
                }
            }
        }
    }

    调用:

    using Bll.Ext.Object;

    namespace ...
    {
            public partial class ...
             {
                  private void BindData(string where)
                  {
                             gv.DataSource = Bll.MyData.GetDataTable();
                             gv.DataSource = gv.DataSource.Select(where);
                             gv.DataBind();
                  }
             }
    }

  • 相关阅读:
    CSS_2
    二柱子与他的计算题
    第一章 类与对象
    深入理解计算机系统之一--整数运算
    指针数组的应用
    对象个数以及构造函数次序
    范式介绍
    内连接与外连接
    TCP三次握手四次分手
    进程间的通信方式
  • 原文地址:https://www.cnblogs.com/liningit/p/4861763.html
Copyright © 2020-2023  润新知