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;
}
《黎明杀机》开发商正开发新的《Serious Sam》游戏 由Devolver发行
超英派遣中心即将登陆Xbox Play Anywhere 深入了解这款独特而备受喜爱的冒险游戏
在《Hunter: The Reckoning – Deathwish》中怎么从猎物蜕变为猎手
《The Expanse: Osiris Reborn》——深入了解玩法 新同伴 封闭测试及发行窗口
Alien Deathstorm 科幻生存与动作结合 打造全新第一人称恐怖混合体验
宝可梦公司已在考虑未来可用口袋妖怪总数达到“或许一万只”
幸存者村庄2
怪可爱的店折相思菜单
怪可爱的店折相思菜单是一款画风治愈的模拟经营佳作,内置折相思菜单解锁多项便利功能
lol电竞经理人手游
lol电竞经理人手游让你可以成为职业的电竞经理人,在游戏中掌控游戏的走向!豪华阵
梦幻奶茶屋小游戏
梦幻奶茶屋是一款主打温馨治愈氛围的休闲经营作品,玩家将化身元气满满的奶茶店店长,
萌宠养成物语无限金币版
萌宠养成物语无限金币版是一款治愈系模拟养成游戏,以可爱卡通画风构建温馨虚拟世界,