c#中合并DataTable重復(fù)行的值
//DataTable數(shù)據(jù)添加
Hashtable ht = new Hashtable();
DataTable dt = new DataTable();
DataColumn dc = new DataColumn("id");
dt.Columns.Add(dc);
dc = new DataColumn("name");
dt.Columns.Add(dc);
dc = new DataColumn("values");
dt.Columns.Add(dc);
DataRow dr = dt.NewRow();
dr["id"] = 1;
dr["name"] = "張三";
dr["values"] = "A";
dt.Rows.Add(dr);
dr = dt.NewRow();
dr["id"] = 2;
dr["name"] = "李四";
dr["values"] = "B";
dt.Rows.Add(dr);
dr = dt.NewRow();
dr["id"] = 3;
dr["name"] = "張三";
dr["values"] = "C";
dt.Rows.Add(dr);
dataGridView1.DataSource = dt;
//合并
for (int i = 0; i < dt.Rows.Count; i++ )
{
if (ht.ContainsKey(dt.Rows[i]["name"]))
{
//獲取行索引
int index = (int)ht[dt.Rows[i]["name"]];
//獲取最近一次的值(對(duì)應(yīng)values)
string str = (string)dt.Rows[index]["values"];
//拼接
dt.Rows[index]["values"] = str + "|" + dt.Rows[i]["values"];
//刪除重復(fù)行
dt.Rows.RemoveAt(i);
//調(diào)整索引減1
i--;
}
else
{
//保存名稱以及行索引
ht.Add(dt.Rows[i]["name"], i);
}
}
- C#使用DataSet Datatable更新數(shù)據(jù)庫(kù)的三種實(shí)現(xiàn)方法
- C#中DataTable排序、檢索、合并等操作實(shí)例
- C#中把Datatable轉(zhuǎn)換為Json的5個(gè)代碼實(shí)例
- C#對(duì)DataTable里數(shù)據(jù)排序的方法
- c#將list類型轉(zhuǎn)換成DataTable方法示例
- C#中DataTable刪除行的方法分析
- C#中datatable去重的方法
- C#將DataTable轉(zhuǎn)換成list的方法
- C#從DataTable獲取數(shù)據(jù)的方法
- C#實(shí)現(xiàn)將DataTable內(nèi)容輸出到Excel表格的方法
- C#中csv文件與DataTable互相導(dǎo)入處理實(shí)例解析
- C# DataTable中Compute方法用法集錦(數(shù)值/字符串/運(yùn)算符/表等操作)
相關(guān)文章
C#?Socket數(shù)據(jù)接收的三種實(shí)現(xiàn)方式
本文主要介紹了C#?Socket數(shù)據(jù)接收的三種實(shí)現(xiàn)方式,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-07-07
C# Onnx實(shí)現(xiàn)輕量實(shí)時(shí)的M-LSD直線檢測(cè)
這篇文章主要為大家詳細(xì)介紹了C#如何結(jié)合Onnx實(shí)現(xiàn)輕量實(shí)時(shí)的M-LSD直線檢測(cè),文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-11-11
C#中通過反射將枚舉元素加載到ComboBo的實(shí)現(xiàn)方法
本文主要介紹了C#中通過反射將枚舉元素加載到ComboBo的實(shí)現(xiàn)方法,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-09-09
C#事件中的兩個(gè)參數(shù)詳解(object sender,EventArgs e)
這篇文章主要介紹了C#事件中的兩個(gè)參數(shù)詳解(object sender,EventArgs e),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-09-09
C#給picturebox控件加圖片選中狀態(tài)的2個(gè)方法
C#給picturebox控件加圖片選中狀態(tài)的2個(gè)方法,需要的朋友可以參考一下2013-03-03

