• DataRow 数组转化成DataTable


    DataRow [] dr = outinfo.Tables[0].Select("stock_in_rec_id=" + this.efGrid1.Rows[this.efGrid1.RowSel]["stock_in_rec_id"].ToString() + "");

     dr.CopyToDataTable();

    有时候需要把dataset其中一个表的内容读取到DataRow,之后再复制到新的datatable应用。下面是实现的代码:

    DataRow[]转换成DataTable的方法:
    DataTable dt=new DataTable();
    DataRow[] dr=new DataRow();
    dr=GetChildRows(...);
    for(int i=0;i<dr.Length;i++)
    {
    dt.ImportRow(dr[i]);
    }
    dg.DataSource=dt;
    dg.DataBind();

    向一个DataTable批量添加DataRow时有两种办法:
    DataTable dt;
    DataTable newdt;

    for(int i = 0;i<dt.Rows.Count;i++)
    {
        newdt.Rows.Add(dt.Rows[i].ItemArray);
    }


    for(int i = 0;i<dt.Rows.Count;i++)
        {
         newdt.ImportRow(dt.Rows[i]);
        
        }

    两种方式速度很快,200条记录,50ms左右。

    但是今天,在实际开发中发现时间在5S,郁闷呀。

    检查代码,发现添加记录的DataTable一直绑定在一个DataGrid,
    改了代码:
    this.DataGrid1.DataSource = null;
    for(int i = 0;i<dt.Rows.Count;i++)
        {
         newdt.ImportRow(dt.Rows[i]);
        
        }
    this.DataGrid1.DataSource = newdt;

    速度重新快了

  • 相关阅读:
    关于代码风格
    python笔记-glob模块
    python笔记-mysql安装与配置
    python笔记-shutil模块
    python笔记-os模块
    python笔记-内存分析
    python笔记-装饰器
    python笔记-正则表达式
    python笔记-迭代器-生成器-对象生成式
    python笔记-模块和包
  • 原文地址:https://www.cnblogs.com/dodui/p/1915871.html
Copyright © 2020-2023  润新知