最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
asp.net中dataset删除重复数据的方法
时间:2011-06-18 编辑:简简单单 来源:一聚教程网
DataTable 中的数据如下:
id key value
1 a aa
1 a aa
2 b bb
2 b bb
3 c cc
3 c cc
现在想要让DataTable变成这个样子的
id key value
1 a aa
2 b bb
3 c cc
代码
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;
}
再看一个实例
///
/// 获取对固定列不重复的新DataTable
///
/// 含有重复数据的DataTable
/// 需要验证重复的列名
///新的DataTable,colName列不重复,表格式保持不变
private DataTable GetDistinctTable(DataTable dt,string colName)
{
DataView dv = dt.DefaultView;
DataTable dtCardNo = dv.ToTable(true, colName);
DataTable Pointdt = new DataTable();
Pointdt = dv.ToTable();
Pointdt.Clear();
for (int i = 0; i < dtCardNo.Rows.Count; i )
{
DataRow dr = dt.Select(colName "='" dtCardNo.Rows[i][0].ToString() "'")[0];
Pointdt.Rows.Add(dr.ItemArray);
}
return Pointdt;
}
-
下一个: .net获取网站路径方法
相关文章
- C#复制数组的两种方式及效率比较解读 10-24
- ASP.NET Identity用法解析 10-24
- ASP.NET MVC使用Identity增删改查用户介绍 10-24
- C语言中atoi函数模拟实现介绍 10-18
- .Net反向代理组件Yarp用法介绍 10-10
- .NET使用YARP通过编码方式配置域名转发实现反向代理教程 10-10