Restricting the number of rows in a dataview when binding to a repeate(geekzilla.co.uk)

submitted by phaymanphayman(3550) 5 years, 9 months ago

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

2 comments |category: |Views: 24

tags: another

new Add a live kick counter to your blog >> liveImage

You can even customize the image by choosing your own colors, and then clicking the button below to update the preview and the html code:

  • "Kick It" text
  • "Kick It" background
  • kick count text
  • kick count background
  • border

Simply copy and paste this HTML into your blog post.


Users who kicked this story:
Comments:

posted by wattleswattles(0) 5 years, 8 months ago 0

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

Reply

posted by wattleswattles(0) 5 years, 8 months ago 0

I'm sorry, I meant datagridview not datagrid.

Reply

information Login or create an account to comment on this story