DataTable Sort (GO)

Source: Internet
Author: User

DataTable 排序

DataRow[] rows = dataTable1.Select( "" , "ord asc" ); DataTable t = DataTable1.Clone(); t.Clear(); foreach (DataRow row in rows)      t.ImportRow(row); DataTable1 = t; VS2005中这种方法最简单: DataView dv = dt.DefaultView; dv.Sort = "c1 Asc" ; DataTable dt2 = dv.ToTable(); -------------------------- //拿到数据源 DataView dv = this .dtDataSource.Copy().DefaultView; //排序 dv.Sort = "款 asc,項 asc,目 asc" ; //过滤重复数据 //DataTable dt = dv.ToTable(true, "款", "項", "目"); //不过滤重复数据 DataTable dt = dv.Table.Copy();   ----------------------------------------------- 关于DataView(DataTable)排序的思考         因项目需要,在获得<a target= "_blank" style= "color: #0000F0; display:inline; position:static; background:none;" href= "http://www.so.com/s?q=dataset&ie=utf-8&src=se_lighten_f" >dataset</a>(或者DataTable)之后要对该table进行重新排序,排序规则按照<a target= "_blank" style= "color: #0000F0; display:inline; position:static; background:none;" href= "http://www.so.com/s?q=%E6%95%B0%E7%BB%84&ie=utf-8&src=se_lighten_f" >数组</a>fids元素的顺序进行。 在测试的时候,我用简单的思考方法,在table上增加一列dis,来存放该行的主键在fids中的对应项的索引,一个循环之后,dis都被赋了值,现在就要排序了,因为datatable没有排序功能,就选择了转换DataTable为DataView,然后对DataView进行排序,关键代码如下: DataView dw = dt.DefaultView; dw.Sort = "dis,发布日期 desc" ; 然后把dw绑定到DataGird输出,结果正好符合原意。 可是具体使用中却出了问题,因为我对dw又进行了一次处理,取该分页需要的某几项绑定到DataGird,于是结果和排序之前一样,没有了变化。 究其原因,才发现,这里的DataView只是设定了排序规则,并没有实施排序,那么排序在什么时候发生呢,应该是在绑定到DataGird的时候才发生,所以我后来的方法就出了问题。 总结如下:DataView的筛选和排序功能都是在绑定到数据控件的时候才执行的,如果你是简单的需求(也就是说不需要再取其中的部分数据来绑定数据控件),那就可以使用该功能。 我的问题的最终解决方法如下(只使用了简单的循环): private DataTable SortTable(DataTable dt, string [] pids)           {                DataTable dt0 = dt.Clone(); //复制原表结构                for ( int i=0;i<pids.Length;i++)                {                     if (pids[i] != string .Empty)                     {                         DataRow[] drs = dt.Select( "pos_id=" + pids[i]);                         if (drs.Length > 0)                         {                              foreach (DataRow dr in drs)                              {                                   dt0.ImportRow(dr); //导入行                              }                         }                     }                }                return dt0;       } 说明:就是对排序的数组循环,在datatable中找对应的行,然后复制到新表中。 该方法的效率还是可以的,不过如果交集次数大于20,000,000的时候,就会有效率问题。 ****************************************************************   dstaset.Tables.Add(dt) dataset.Tables(0).DefaultView.Sort = "id desc" dv = New DataView(dt) dv.Sort = "id desc" dv = New DataView(ds.Tables[0]) dv.Sort = "id desc" -------------------------------------------------------------- System.Data.DataTable table = new System.Data.DataTable();              table.Columns.Add( "aa" , typeof ( string ));              System.Data.DataRow row = table.NewRow();              row[ "aa" ] = "sdf" ;              table.Rows.Add(row);              System.Data.DataView dv = new System.Data.DataView(table);              table.Columns.Add( "bb" , typeof ( string )); DataTable dt = con.getDGResulthk(sql);              DataColumn col1 = con.getCol(sql1);              DataColumn col = dt.Columns.Add( "工资" );              DataRow dr = dt.NewRow();              dr[ "工资" ] = col1;              dt.Rows.Add(dr);              dataGridView1.DataSource = dt;                           dataGridView1.AutoResizeColumns();   System.Data.DataTable table = new System.Data.DataTable();              table.Columns.Add( "aa" , typeof ( string ));              System.Data.DataRow row = table.NewRow();              row[ "aa" ] = "sdf" ;              table.Rows.Add(row);              System.Data.DataView dv = new System.Data.DataView(table);     DataTable drReport= new DataTable(); DataRow[] pdrs111 = dtReport.Select( "code=‘‘" + strNoCode + "‘‘" );                          if (pdrs111.Length > 0)                          {                              foreach (DataRow dr111 in pdrs111)                              {                                  decfd_1 = Convert.ToDouble(dr111[ "fd" ].ToString());                                  dr111[ "fd_no" ] = dtNOValue;                                  dr111[ "fd_no_toal" ] = decfd_1 + dtNOValue;                              }                          }     dstaset.Tables.Add(dt) dataset.Tables(0).DefaultView.Sort = "id desc" dv = New DataView(ds.Tables[0]) dv.Sort = "id desc"From:http://www.cnblogs.com/xiaofengfeng/archive/2012/06/27/2565941.html

DataTable Sort (GO)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.