如何使用C#删除数据表中的上一行 [英] How to delete the previous row in data table using C#

查看:95
本文介绍了如何使用C#删除数据表中的上一行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好我的应用程序我将数据库中的数据从数据表中恢复到数据表中我想删除数据表中的上一行。



我尝试了什么:



假设我的排在第1排(1,约翰,2000)第2排(2,结婚,21000),第3排(1,john,22000)这里我要删除john的重复记录意味着我要删除第一行并显示3行。

解决方案

删除重复使用以下代码



  public  DataTable RemoveDuplicateRows(DataTable dTable, string  colName)
{
Hashtable hTable = new Hashtable();
ArrayList duplicateList = new ArrayList();

// 将所有唯一商品值的列表添加到哈希表,其中存储密钥的组合,值对。
// 并在arraylist中添加重复的项目值。
foreach (DataRow drow in dTable.Rows)
{
if (hTable.Contains(drow [colName]))
duplicateList.Add(drow);
else
hTable.Add(drow [colName], string .Empty) ;
}

// 从数据表中删除重复项目列表。
foreach (DataRow dRow in duplicateList)
dTable.Rows.Remove (奇幻);

// 包含唯一记录的数据表将作为输出返回。
return dTable;
}


检查此帖子 c# - 从数据表中删除重复条目的最佳方法 - Stack Overflow [ ^ ]它有不同的解决方案,选择你需要的解决方案。

hello in my application i retriving the data from database in to the data table here i want to delete the previous row from the data table.

What I have tried:

suppose my row are following 1st row(1,john,2000) 2nd row (2,marry,21000), 3rd (1,john,22000) here i want to delete the duplicate record of john means i want to delete the first row and display the 3 row .

解决方案

Remove duplicate using following code

public DataTable RemoveDuplicateRows(DataTable dTable, string colName)
{
   Hashtable hTable = new Hashtable();
   ArrayList duplicateList = new ArrayList();

   //Add list of all the unique item value to hashtable, which stores combination of key, value pair.
   //And add duplicate item value in arraylist.
   foreach (DataRow drow in dTable.Rows)
   {
      if (hTable.Contains(drow[colName]))
         duplicateList.Add(drow);
      else
         hTable.Add(drow[colName], string.Empty); 
   }

   //Removing a list of duplicate items from datatable.
   foreach (DataRow dRow in duplicateList)
      dTable.Rows.Remove(dRow);

   //Datatable which contains unique records will be return as output.
      return dTable;
}


check this thread c# - Best way to remove duplicate entries from a data table - Stack Overflow[^] it has different solution, choose the one you need.


这篇关于如何使用C#删除数据表中的上一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆