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();
                  }
             }
    }

  • 相关阅读:
    [国家集训队]墨墨的等式(同余最短路)
    [洛谷P2575]高手过招
    [CSP校内集训]rank
    杀人游戏(tarjan思维好题)
    骑士游戏(spfa好题)
    机房模拟测试4:计数类dp+水题+树上计数
    机房测试模拟2:模拟+数学+数位dp
    机房测试11:最小生成树(最小生成树+二分)
    机房测试模拟1(day2):矩阵+树上贪心+bfs+状压
    机房测试16:字符串专题(AC自动机+dp+kmp)
  • 原文地址:https://www.cnblogs.com/liningit/p/4861763.html
Copyright © 2011-2022 走看看