void DeleteSameRow(DataSet ds)
{
ArrayList indexList = new ArrayList();
// 找出待删除的行索引
for (int i = 0; i < ds.Tables[0].Rows.Count-1; i++)
{
if (!IsContain(indexList, i))
{
for (int j = i + 1; j < ds.Tables[0].Rows.Count; j++)
{
if (ds.Tables[0].Rows[i][AccountInfo.Columns.AUName].ToString() == ds.Tables[0].Rows[j][AccountInfo.Columns.AUName].ToString())
{
indexList.Add(j);
}
}
}
}
// 根据待删除索引列表删除行
for (int i = indexList.Count - 1; i >= 0; i--)
{
int index = Convert.ToInt32(indexList[i]);
ds.Tables[0].Rows.RemoveAt(index);
}
}
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;
}
{
ArrayList indexList = new ArrayList();
// 找出待删除的行索引
for (int i = 0; i < ds.Tables[0].Rows.Count-1; i++)
{
if (!IsContain(indexList, i))
{
for (int j = i + 1; j < ds.Tables[0].Rows.Count; j++)
{
if (ds.Tables[0].Rows[i][AccountInfo.Columns.AUName].ToString() == ds.Tables[0].Rows[j][AccountInfo.Columns.AUName].ToString())
{
indexList.Add(j);
}
}
}
}
// 根据待删除索引列表删除行
for (int i = indexList.Count - 1; i >= 0; i--)
{
int index = Convert.ToInt32(indexList[i]);
ds.Tables[0].Rows.RemoveAt(index);
}
}
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;
}
本文介绍了一种使用C#实现的从DataSet中删除重复行的方法。该方法通过比较特定列(AUName)的数据来识别重复项,并利用ArrayList记录需要删除的行索引,最后按索引逆序删除这些行,确保不会因行删除导致的索引变动而遗漏数据。
136

被折叠的 条评论
为什么被折叠?



