我有一个asp.net数据表,我想将数据绑定到两个asp.net数据表中,所以我想将两个数据表中的数据表行切成两个相同的大小,如果是偶数的话。
答案 0 :(得分:2)
答案 1 :(得分:0)
如果按行切片,您只需创建原始数据表的副本,找到合适的中间点,然后将行导入副本,同时将其从原始数据中删除。
以下内容应该有效:
DataTable originalTable = new DataTable();
//Load the data into your original table or wherever you get your original table from
DataTable otherTable = originalTable.Copy(); //Copys the table structure only - no data
int rowCount = originalTable.Rows.Count;
int wayPoint = rowCount / 2; //NB integer division rounds down towards 0
for(int i = 0; i <= wayPoint; i++)
{
otherTable.ImportRow(originalTable.Rows[i]); //Imports (copies) the row from the original table to the new one
originalTable.Rows[i].Delete(); //Marks row for deletion
}
originalTable.AcceptChanges(); //Removes the rows we marked for deletion