viernes, abril 19, 2024

Contador de filas para un DataGrid

Como muchos sabrán en .Net no hay un método definido para contar las filas que muestra un DataGrid.

Hay miles de formas para contar las filas, ya sea contando las rows en la tabla del dataset, etc. Pero en este caso les enseño una función que hice para contar las filas de un DataGrid.

La función retorna un integer que es el conteo de las líneas, el nombre del DataGrid es dtgPrueba, ahí hay q reemplazarlo por el datagrid a utilizar.

Private Function cuentaLineasdtgPrueba() As Integer
Dim cont As Integer = 0
Dim indiceActual As Integer = 0
Dim indiceAnterior As Integer = -1
Dim indiceRID As Integer = Me.dtgPrueba.CurrentRowIndex

Try
If (Me.dtgPrueba.CurrentRowIndex <> -1) Then
If (Me.dtgPrueba.VisibleRowCount <> -1) Then
Me.dtgPrueba.CurrentRowIndex = 0

While (indiceActual <> indiceAnterior)
indiceAnterior = indiceActual

Me.dtgPrueba.CurrentRowIndex = Me.dtgPrueba.CurrentRowIndex + 1
indiceActual = Me.dtgPrueba.CurrentRowIndex
cont = cont + 1
End While

Me.dtgPrueba.CurrentRowIndex = indiceRID
End If
End If
Catch ex As Exception
Throw ex
End Try

Return cont
End Function

Roy Rojas
Roy Rojashttp://www.dotnetcr.com
Con más de 20 años de experiencia en programación, experto en lenguajes .NET, VB, C#, ASP.NET, Xamarin, XCode, DBA en SQL Server. Creador de dotnetcr.com, sitio web para programadores en español. royrojas.com | dotnetcr.com | GitHub
Roy Rojas
Roy Rojashttp://www.dotnetcr.com
Con más de 20 años de experiencia en programación, experto en lenguajes .NET, VB, C#, ASP.NET, Xamarin, XCode, DBA en SQL Server. Creador de dotnetcr.com, sitio web para programadores en español. royrojas.com | dotnetcr.com | GitHub