绑定数组对象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();
}
}
}

 

你可能感兴趣的:(Datatable)