您的位置:首页 > 其它

如何去掉DataTable中的重复行(新增.net 2.0中最新解决方法---简便) (转)

2008-06-23 14:46 841 查看
.net 1.1中的解决方法(转)

1建立一个DataSetHelper类(DataSetHelper.cs)

public class DataSetHelper

DataSet ds;

DataSetHelper dsHelper;

ds = new DataSet();

dsHelper = new DataSetHelper(ref ds);

// Create source table

DataTable dt = new DataTable("Orders");

dt.Columns.Add("EmployeeID", Type.GetType("System.String"));

dt.Columns.Add("OrderID", Type.GetType("System.Int32"));

dt.Columns.Add("Amount", Type.GetType("System.Decimal"));

ds.Tables.Add(dt);

DataTable td=dsHelper.SelectDistinct("DistinctEmployees", ds.Tables["Orders"], "EmployeeID");

this.GridView1.DataSource = td;

this.GridView1.DataBind();

.net 2.0中的解决方法(原创)

public DataTable GetTopSearch()

{

{

string keyword = dsKeyword.Tables[0].Rows[i]["Name"].ToString();

string condition = dsKeyword.Tables[0].Rows[i]["SearchCondition"].ToString();

dsTopSearch.Merge(dal.GetTopSearch(keyword,condition));

}

return dsTopSearch.Tables[0].DefaultView.ToTable(true, "ID","Name","Author","Publisher","PublishDate","TypeName","Price","SalePrice","SavePrice","Rebate","ImagePath","ContentIntro");

}

先把DataTable转成DataView,再通过DataView.ToTable()转回DataTable,ToTable()方法中有一个重载可以轻松消除重复行.

注:该重载的第二个参数为要保存的字段名.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: