我想将数据表拆分为N个部分。让我们假设N = 5。
如果datatable.rows.count为13,则数据表应拆分为3组 其中:
如果datatable.rows.count为16,则数据表应拆分为4组,其中:
怎么可能?在网上搜索时,我知道它可以像Split a collection into `n` parts with LINQ?
那样实现但我想做一个简单的函数,其中传递数据表和N的值。
在循环内循环会让人感到困惑。
答案 0 :(得分:0)
'create a dtb for demo
Dim dtbSource As New DataTable("MyDataTable")
dtbSource.Columns.Add("Column1", GetType(String))
dtbSource.Columns.Add("Column2", GetType(String))
dtbSource.Columns.Add("Column3", GetType(String))
dtbSource.Rows.Add("1", "2", "3")
dtbSource.Rows.Add("2", "2", "3")
dtbSource.Rows.Add("3", "2", "3")
dtbSource.Rows.Add("4", "2", "3")
dtbSource.Rows.Add("5", "2", "3")
dtbSource.Rows.Add("6", "2", "3")
dtbSource.Rows.Add("7", "2", "3")
dtbSource.Rows.Add("8", "2", "3")
dtbSource.Rows.Add("9", "2", "3")
dtbSource.Rows.Add("10", "2", "3")
dtbSource.Rows.Add("11", "2", "3")
dtbSource.Rows.Add("12", "2", "3")
dtbSource.Rows.Add("13", "2", "3")
'now split the datatable
Dim n As Integer = 5 'number of rows per datatable
Dim dst As New DataSet("Output")
For i As Integer = 0 To dtbSource.Rows.Count - 1 Step n
Dim intLastRow As Integer = i + n - 1
If intLastRow > dtbSource.Rows.Count - 1 Then intLastRow = dtbSource.Rows.Count - 1
Dim dtbNew As DataTable = dtbSource.Clone 'copy structure of original datatable
dtbNew.TableName = dtbSource.TableName & "_" & (i \ n).ToString
For j As Integer = i To intLastRow
dtbNew.ImportRow(dtbSource.Rows(j))
Next j
dst.Tables.Add(dtbNew)
Next i
'the split datatables are available in dst.Tables
MsgBox(dst.Tables(1).Rows(2).Item(0).ToString) 'table 2, row 3, column 1
答案 1 :(得分:0)
这可能对您想要的内容有帮助
Dim page As Integer = 0
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim data As New DataClasses1DataContext
Dim m = (From master In data.Masters Select master).Skip(page * 5).Take(5)
DataGridView1.DataSource = m
page += 1
End Sub