简体   繁体   English

如何将数据集平均分为两部分?

[英]How can I divide a dataset equally into two parts?

I'm programming on Asp.net and C#. 我在Asp.net和C#上编程。

I have a Dataset which is populated by records from database. 我有一个数据集,该数据集由数据库中的记录填充。 On my design page, I have two Datagrids. 在我的设计页面上,我有两个Datagrids。

How can I equally divide the records inside the Dataset so they can be bound to two separate datagrids? 如何在数据集中平均划分记录,以便将它们绑定到两个单独的数据网格?

you can use DataView on your dataset and then bind your 2 Datagrids on dataview here a sample http://www.dotnetperls.com/dataview 您可以在数据集上使用DataView,然后将两个Datagrids绑定到dataview上,此处为示例http://www.dotnetperls.com/dataview

work for instance to set one dataview on rows.count/2 and the other too 例如工作在rows.count / 2上设置一个数据视图,另一个

This works for me, 这对我有用

 var d=ds.Tables[0];// here ds is your dataset.
 int count=d.Rows.Count;
 var x=new DataTable();
 for(int i=0;i<=count;i++)
 {
   var dr=d.Rows[i];   
   x.Rows.Add(dr.ItemArray);
   d.Rows.RemoveAt(i); 
 } 
 var ret=new DataSet();
 ret.Tables.Add(x);
 ret.Tables.Add(d);

so now you have dataset that contain two equal datatable. 所以现在您有了包含两个相等数据表的数据集。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM