Sunday May 1st

Friday April 29th

Thursday April 28th

Wednesday April 27th

Restricting the number of rows in a dataview when binding to a repeate

Using IEnumerable to limit the number of rows in a DataView when binding to a repeater.

2 comments

I can't get this to work with my ap using VB and a datagrid. I think the class is working property as I added a binding source as an intermediary and am getting the correct number of rows back.


Here is my function call:

Private Sub test()
Dim ds As DataSet
Dim dv As DataView
Dim dr As DataRow


ds = New DataSet("Testing")
ds.Tables.Add("Test")


ds.Tables("Test").Columns.Add(New DataColumn("col1"))

dv = New DataView(ds.Tables("Test"))
dv.AllowDelete = True

dr = ds.Tables("Test").NewRow
dr.Item(0) = "1"
ds.Tables("Test").Rows.Add(dr)
dr = ds.Tables("Test").NewRow
dr.Item(0) = "2"
ds.Tables("Test").Rows.Add(dr)
dr = ds.Tables("Test").NewRow
dr.Item(0) = "3"
ds.Tables("Test").Rows.Add(dr)


Me.BindingSource1.DataSource = New FilterRows(dv, 2)
Debug.WriteLine("binding source count " & Me.BindingSource1.Count)

Me.DataGridView1.DataSource = dv

End Sub

'And here is the class converted to vb


Imports System
Imports System.Data
Imports System.Collections

Namespace transitFX.Display


'/ <summary>
'/ Summary description for FilterRows
'/ </summary>
Public Class FilterRows
Implements IEnumerable 'ToDo: Add Implements Clauses for implementation methods of these interface(s)
Private dataView As DataView
Private rowsToShow As Integer


Public Sub New(ByVal dataView As DataView, ByVal rowsToShow As Integer)
Me.rowsToShow = rowsToShow
Me.dataView = dataView
Debug.WriteLine("called")
End Sub 'New


Public Function GetEnumerator() As IEnumerator Implements IEnumerable.GetEnumerator

Return New PageOfData(Me.dataView.GetEnumerator(), Me.rowsToShow)
End Function 'GetEnumerator
_


Friend Class PageOfData
Implements IEnumerator 'ToDo: Add Implements Clauses for implementation methods of these interface(s)
Private e As IEnumerator
Private cnt As Integer = 0
Private rowsToShow As Integer


Friend Sub New(ByVal e As IEnumerator, ByVal rowsToShow As Integer)
Me.rowsToShow = rowsToShow
Me.e = e
End Sub 'New


Public ReadOnly Property Current() As Object Implements IEnumerator.Current
Get
Return e.Current
End Get
End Property

Public Function MoveNext() As Boolean Implements IEnumerator.MoveNext
' If we've hit out limit return false
If cnt >= rowsToShow Then
Return False
End If
' Track the current row
cnt += 1

Return e.MoveNext()
End Function 'MoveNext


Public Sub Reset() Implements IEnumerator.Reset
e.Reset()
cnt = 0
End Sub 'Reset
End Class 'PageOfData
End Class 'FilterRows
End Namespace

I'm sorry, I meant datagridview not datagrid.

Commenting on Stories is limited for now and will open up to those recommended by the community. Learn how
Loading DotNetKicks...
brought to you by the Kicks Network