VS2005 has been encapsulated for the DataTable to repeat the method:
Copy Code code as follows:
Remove Duplicate rows
DataView dv = table. DefaultView;
Table = dv. ToTable (True, new string[] {"Name", "Code"});
At this point, the table has only two lines of name, code without duplicates, and if the ID value is required
Table = dv. ToTable (True, new string[] {"id", "name", "Code"});//First argument true enable to repeat, similar to distinct
If you have a set of data (ID is not a unique field)
Copy Code code as follows:
ID Name Code
Zhang 3,123
Lee 4,456
Zhang 3,456
Zhang 3,123
Through the above methods to get
Copy Code code as follows:
ID Name Code
Zhang 3,123
Lee 4,456
Zhang 3,456
To repeat the removal of only the row where ID name code is completely duplicated, what if the data you want to filter is only name not allowed to repeat?
Copy Code code as follows:
Table = dv. ToTable (True, new string[] {"Name"});
Get:
Copy Code code as follows:
But the result I want is to repeat only for one of the column name columns, and to show the other columns
The results required are:
Copy Code code as follows:
ID Name Code
1 Sheets 3,123
2 Lee 4,456
How does this happen? The following method can be, there may be a better way, I hope that we have a lot of advice
Copy Code code as follows:
#region Delete DataTable repeating columns, similar to distinct
<summary>
Delete DataTable repeating column, similar to distinct
</summary>
<param name= "DT" >DataTable</param>
<param name= "field" > Field name </param>
<returns></returns>
public static DataTable Deletesamerow (DataTable DT, String Field)
{
ArrayList indexlist = new ArrayList ();
Find the row index to delete
for (int i = 0; i < dt. rows.count-1; i++)
{
if (! Iscontain (Indexlist, i))
{
for (int j = i + 1; j < dt.) Rows.Count; J + +)
{
if (dt. Rows[i][field]. ToString () = = dt. Rows[j][field]. ToString ())
{
Indexlist.add (j);
}
}
}
}
Delete rows based on the list of indexes to delete
for (int i = indexlist.count-1 i >= 0; i--)
{
int index = Convert.ToInt32 (Indexlist[i]);
Dt. Rows.removeat (index);
}
return DT;
}
<summary>
To determine if the array exists
</summary>
<param name= "indexlist" > Array </param>
<param name= "index" > Index </param>
<returns></returns>
public static bool Iscontain (ArrayList indexlist, int index)
{
for (int i = 0; i < Indexlist.count; i++)
{
int tempindex = Convert.ToInt32 (Indexlist[i]);
if (Tempindex = = index)
{
return true;
}
}
return false;
}
#endregion