zoukankan      html  css  js  c++  java
  • 绑定数组对象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();
                  }
             }
    }

  • 相关阅读:
    less 28-31
    less27 27a
    sqli 26 26a
    sqli lab 25 25a
    kail 更新源
    sqli lab 23 、24
    less 20 21 22
    less18 19
    less 17
    数字类型,字符串类型,列表类型
  • 原文地址:https://www.cnblogs.com/liningit/p/4861763.html
Copyright © 2011-2022 走看看