zoukankan      html  css  js  c++  java
  • DataTable合并重复行的值

                //DataTable数据添加
    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"]];
    //获取最近一次的值(对应values)
    string str = (string)dt.Rows[index]["values"];
    //拼接
    dt.Rows[index]["values"] = str + "|" + dt.Rows[i]["values"];
    //删除重复行
    dt.Rows.RemoveAt(i);
    //调整索引减1
    i--;
    }
    else
    {
    //保存名称以及行索引
    ht.Add(dt.Rows[i]["name"], i);
    }
                }
    code in my life.
  • 相关阅读:
    An AODV Tutorial
    MFC去掉单文档的"无标题-"的方法
    win32 openss 编译
    ASP.NET实现RENREN SIG计算
    std::string str.c_str() const
    fopen
    curl with ssl support for win32
    VC++ utf8 Unicode GB2312 编码转换
    编码转换
    VirtualBox uuid冲突问题
  • 原文地址:https://www.cnblogs.com/ghypnus/p/2433271.html
Copyright © 2011-2022 走看看