.net 1.1中的解决方法(转)
1建立一个DataSetHelper类(DataSetHelper.cs)
public class DataSetHelper
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
...{
public DataSet ds;
public DataSetHelper(ref DataSet DataSet)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
ds = DataSet;
}
public DataSetHelper()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
ds = null;
}
private bool ColumnEqual(object A, object B)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
if (A == DBNull.Value && B == DBNull.Value) // both are DBNull.Value
return true;
if (A == DBNull.Value || B == DBNull.Value) // only one is DBNull.Value
return false;
return (A.Equals(B)); // value type standard comparison
}
public DataTable SelectDistinct(string TableName, DataTable SourceTable, string FieldName)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
DataTable dt = new DataTable(TableName);
dt.Columns.Add(FieldName, SourceTable.Columns[FieldName].DataType);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
object LastValue = null;
foreach (DataRow dr in SourceTable.Select("", FieldName))
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
if (LastValue == null || !(ColumnEqual(LastValue, dr[FieldName])))
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
LastValue = dr[FieldName];
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
dt.Rows.Add(new object[] ...{ LastValue });
}
}
if (ds != null)
ds.Tables.Add(dt);
return dt;
}
}
2 建立一个Web窗体,在page_load中写下面的代码
DataSet ds;
DataSetHelper dsHelper;
ds = new DataSet();
dsHelper = new DataSetHelper(ref ds);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
// 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"));
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
dt.Rows.Add(new object[] ...{ "Sam", 5, 25.00 });
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
dt.Rows.Add(new object[] ...{ "Tom", 7, 50.00 });
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
dt.Rows.Add(new object[] ...{ "Sue", 9, 11.00 });
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
dt.Rows.Add(new Object[] ...{ "Tom", 12, 7.00 });
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
dt.Rows.Add(new Object[] ...{ "Sam", 14, 512.00 });
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
dt.Rows.Add(new Object[] ...{ "Sue", 15, 17.00 });
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
dt.Rows.Add(new Object[] ...{ "Sue", 22, 2.50 });
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
dt.Rows.Add(new object[] ...{ "Tom", 24, 3.00 });
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
dt.Rows.Add(new object[] ...{ "Tom", 33, 78.75 });
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
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()
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
DataSet dsKeyword = dal.GetKeyword();
DataSet dsTopSearch = new DataSet();
for (int i = 0; i < 4; i++)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
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()方法中有一个重载可以轻松消除重复行.
注:该重载的第二个参数为要保存的字段名.