DataTable.Select 方法 [C#]请参见 DataTable 类 | DataTable 成员 | System.Data 命名空间 | 代码:查询数据表 (Visual Basic) | C++ 托管扩展编程 语言 C#C++JScriptVisual Basic全部显示 获取 DataRow 对象的数组。 重载列表 获取所有 DataRow 对象的数组。 受 .NET Framework 精简版的支持。 [Visual Basic] Overloads Public Function Select() As DataRow() [C#] public DataRow[] Select(); [C++] public: DataRow* Select() []; [JScript] public function Select() : DataRow[]; 按照主键顺序(如果没有主键,则按照添加顺序)获取与筛选条件相匹配的所有 DataRow 对象的数组。 受 .NET Framework 精简版的支持。 [Visual Basic] Overloads Public Function Select(String) As DataRow() [C#] public DataRow[] Select(string); [C++] public: DataRow* Select(String*) []; [JScript] public function Select(String) : DataRow[]; 获取按照指定的排序顺序且与筛选条件相匹配的所有 DataRow 对象的数组。 受 .NET Framework 精简版的支持。 [Visual Basic] Overloads Public Function Select(String, String) As DataRow() [C#] public DataRow[] Select(string, string); [C++] public: DataRow* Select(String*, String*) []; [JScript] public function Select(String, String) : DataRow[]; 获取与排序顺序中的筛选器以及指定的状态相匹配的所有 DataRow 对象的数组。 受 .NET Framework 精简版的支持。 [Visual Basic] Overloads Public Function Select(String, String, DataViewRowState) As DataRow() [C#] public DataRow[] Select(string, string, DataViewRowState); [C++] public: DataRow* Select(String*, String*, DataViewRowState) []; [JScript] public function Select(String, String, DataViewRowState) : DataRow[]; 示例 [Visual Basic, C#] 以下示例使用筛选表达式和记录状态来返回 DataRow 对象的数组。 [Visual Basic, C#] 注意 此示例显示如何使用 Select 的一个重载版本。有关其他可用示例,请参阅单独的重载主题。 [Visual Basic] Private Sub GetRowsByFilter()
Dim customerTable As DataTable customerTable = new DataTable( "Customers" ) ' Add columns customerTable.Columns.Add( "id", GetType(Integer) ) customerTable.Columns.Add( "name", GetType(String) ) ' Set PrimaryKey customerTable.Columns("id").Unique = true customerTable.PrimaryKey = new DataColumn() { customerTable.Columns("id") } ' add ten rows Dim id As Integer For id = 1 To 10 customerTable.Rows.Add( _ new object() { id, string.Format("customer{0}", id) } ) Next id customerTable.AcceptChanges() ' add another ten rows For id = 11 To 20 customerTable.Rows.Add( _ new object() { id, string.Format("customer{0}", id) } ) Next id Dim strExpr As String Dim strSort As String
strExpr = "id > 5" ' Sort descending by CompanyName column. strSort = "name DESC" ' Use the Select method to find all rows matching the filter. Dim foundRows As DataRow() = _ customerTable.Select( strExpr, strSort, DataViewRowState.Added )
PrintRows( foundRows, "filtered rows") foundRows = customerTable.Select() PrintRows( foundRows, "all rows") End SubPrivate Sub PrintRows( rows() As DataRow, label As String) Console.WriteLine( "\n{0}", label ) If rows.Length <= 0 Then Console.WriteLine( "no rows found" ) Exit Sub End If Dim r As DataRow Dim c As DataColumn For Each r In rows For Each c In r.Table.Columns Console.Write( "\t {0}", r(c) ) Next c Console.WriteLine() Next r End Sub [C#] private static void GetRowsByFilter() {
DataTable customerTable = new DataTable( "Customers" ); // Add columns customerTable.Columns.Add( "id", typeof(int) ); customerTable.Columns.Add( "name", typeof(string) ); // Set PrimaryKey customerTable.Columns[ "id" ].Unique = true; customerTable.PrimaryKey = new DataColumn[] { customerTable.Columns["id"] }; // Add ten rows for( int id=1; id<=10; id++ ) { customerTable.Rows.Add( new object[] { id, string.Format("customer{0}", id) } ); } customerTable.AcceptChanges(); // Add another ten rows for( int id=11; id<=20; id++ ) { customerTable.Rows.Add( new object[] { id, string.Format("customer{0}", id) } ); } string strExpr; string strSort;
strExpr = "id > 5"; // Sort descending by column named CompanyName. strSort = "name DESC"; // Use the Select method to find all rows matching the filter. DataRow[] foundRows = customerTable.Select( strExpr, strSort, DataViewRowState.Added );
这里有一个取distinct数据的方法.static DataTable SelectDistinct(string ReturnTableName, DataTable SourceTable, string ReturnFieldName, string AdditionalFilterExpression) { DataTable dt = new DataTable(ReturnTableName); dt.Columns.Add(ReturnFieldName, SourceTable.Columns[ReturnFieldName].DataType); object LastValue = null; foreach (DataRow dr in SourceTable.Select("", ReturnFieldName)) { if (LastValue == null || !(ColumnEqual(LastValue, dr[ReturnFieldName]))) { LastValue = dr[ReturnFieldName]; dt.Rows.Add(new object[] { LastValue }); } } if (ds != null) ds.Tables.Add(dt); return dt; }static bool ColumnEqual(object A, object B) { // Compares two values to see if they are equal. Also compares DBNULL.Value. // Note: If your DataTable contains object fields, then you must extend this // function to handle them in a meaningful way if you intend to group on them.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 }
string myStr="";
for(int i=0;i<dg.Items.Count;i++)
{
if(myStr.IndexOf(dg.Items[i].cells[你要的列].Text)<0)
{
myStr+=dg.Items[i].cells[你要的列].Text+",";
}
}最后得到的myStr就是你需要的不重复的数据。
该程序没有经过测试,你自己再改改。
楼主用的是DataTable 啊. hdt(倦怠) ( ) :
能不能给解释一下.
DataTable 类 | DataTable 成员 | System.Data 命名空间 | 代码:查询数据表 (Visual Basic) | C++ 托管扩展编程 语言
C#C++JScriptVisual Basic全部显示
获取 DataRow 对象的数组。
重载列表
获取所有 DataRow 对象的数组。
受 .NET Framework 精简版的支持。
[Visual Basic] Overloads Public Function Select() As DataRow()
[C#] public DataRow[] Select();
[C++] public: DataRow* Select() [];
[JScript] public function Select() : DataRow[];
按照主键顺序(如果没有主键,则按照添加顺序)获取与筛选条件相匹配的所有 DataRow 对象的数组。
受 .NET Framework 精简版的支持。
[Visual Basic] Overloads Public Function Select(String) As DataRow()
[C#] public DataRow[] Select(string);
[C++] public: DataRow* Select(String*) [];
[JScript] public function Select(String) : DataRow[];
获取按照指定的排序顺序且与筛选条件相匹配的所有 DataRow 对象的数组。
受 .NET Framework 精简版的支持。
[Visual Basic] Overloads Public Function Select(String, String) As DataRow()
[C#] public DataRow[] Select(string, string);
[C++] public: DataRow* Select(String*, String*) [];
[JScript] public function Select(String, String) : DataRow[];
获取与排序顺序中的筛选器以及指定的状态相匹配的所有 DataRow 对象的数组。
受 .NET Framework 精简版的支持。
[Visual Basic] Overloads Public Function Select(String, String, DataViewRowState) As DataRow()
[C#] public DataRow[] Select(string, string, DataViewRowState);
[C++] public: DataRow* Select(String*, String*, DataViewRowState) [];
[JScript] public function Select(String, String, DataViewRowState) : DataRow[];
示例
[Visual Basic, C#] 以下示例使用筛选表达式和记录状态来返回 DataRow 对象的数组。
[Visual Basic, C#] 注意 此示例显示如何使用 Select 的一个重载版本。有关其他可用示例,请参阅单独的重载主题。
[Visual Basic]
Private Sub GetRowsByFilter()
Dim customerTable As DataTable
customerTable = new DataTable( "Customers" ) ' Add columns
customerTable.Columns.Add( "id", GetType(Integer) )
customerTable.Columns.Add( "name", GetType(String) ) ' Set PrimaryKey
customerTable.Columns("id").Unique = true
customerTable.PrimaryKey = new DataColumn() { customerTable.Columns("id") } ' add ten rows
Dim id As Integer
For id = 1 To 10
customerTable.Rows.Add( _
new object() { id, string.Format("customer{0}", id) } )
Next id
customerTable.AcceptChanges() ' add another ten rows
For id = 11 To 20
customerTable.Rows.Add( _
new object() { id, string.Format("customer{0}", id) } )
Next id Dim strExpr As String
Dim strSort As String
strExpr = "id > 5"
' Sort descending by CompanyName column.
strSort = "name DESC"
' Use the Select method to find all rows matching the filter.
Dim foundRows As DataRow() = _
customerTable.Select( strExpr, strSort, DataViewRowState.Added )
PrintRows( foundRows, "filtered rows") foundRows = customerTable.Select()
PrintRows( foundRows, "all rows")
End SubPrivate Sub PrintRows( rows() As DataRow, label As String)
Console.WriteLine( "\n{0}", label )
If rows.Length <= 0 Then
Console.WriteLine( "no rows found" )
Exit Sub
End If
Dim r As DataRow
Dim c As DataColumn
For Each r In rows
For Each c In r.Table.Columns
Console.Write( "\t {0}", r(c) )
Next c
Console.WriteLine()
Next r
End Sub
[C#]
private static void GetRowsByFilter()
{
DataTable customerTable = new DataTable( "Customers" );
// Add columns
customerTable.Columns.Add( "id", typeof(int) );
customerTable.Columns.Add( "name", typeof(string) ); // Set PrimaryKey
customerTable.Columns[ "id" ].Unique = true;
customerTable.PrimaryKey = new DataColumn[] { customerTable.Columns["id"] }; // Add ten rows
for( int id=1; id<=10; id++ )
{
customerTable.Rows.Add(
new object[] { id, string.Format("customer{0}", id) } );
}
customerTable.AcceptChanges(); // Add another ten rows
for( int id=11; id<=20; id++ )
{
customerTable.Rows.Add(
new object[] { id, string.Format("customer{0}", id) } );
} string strExpr;
string strSort;
strExpr = "id > 5";
// Sort descending by column named CompanyName.
strSort = "name DESC";
// Use the Select method to find all rows matching the filter.
DataRow[] foundRows =
customerTable.Select( strExpr, strSort, DataViewRowState.Added );
PrintRows( foundRows, "filtered rows" ); foundRows = customerTable.Select();
PrintRows( foundRows, "all rows" );
}private static void PrintRows( DataRow[] rows, string label )
{
Console.WriteLine( "\n{0}", label );
if( rows.Length <= 0 )
{
Console.WriteLine( "no rows found" );
return;
}
foreach( DataRow r in rows )
{
foreach( DataColumn c in r.Table.Columns )
{
Console.Write( "\t {0}", r[c] );
}
Console.WriteLine();
}
}
我的意思是说要像sql语句中的distinct一样
{
DataTable dt = new DataTable(ReturnTableName);
dt.Columns.Add(ReturnFieldName, SourceTable.Columns[ReturnFieldName].DataType);
object LastValue = null;
foreach (DataRow dr in SourceTable.Select("", ReturnFieldName))
{
if (LastValue == null || !(ColumnEqual(LastValue, dr[ReturnFieldName])))
{
LastValue = dr[ReturnFieldName];
dt.Rows.Add(new object[] { LastValue });
}
}
if (ds != null)
ds.Tables.Add(dt);
return dt;
}static bool ColumnEqual(object A, object B)
{
// Compares two values to see if they are equal. Also compares DBNULL.Value.
// Note: If your DataTable contains object fields, then you must extend this
// function to handle them in a meaningful way if you intend to group on them.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
}