Delete a DataTable repeating column, delete only one of the columns duplicate rows method _ Practical Tips

Source: Internet
Author: User

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:

Name

Tom

John doe

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.