Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
How many rows in a datagrid?
Message
General information
Forum:
ASP.NET
Category:
Forms
Miscellaneous
Thread ID:
00844332
Message ID:
00844409
Views:
14
Not as pretty as it could be but this worked in my testing. Against a table with 50,000 records this method took 1.78 seconds and against 20 records it took 1.77 seconds ???. So the loop isn't the performance bottleneck. But this is a quick and dirty answer. * I included my full test code at the bottom of this post.
    Friend Function DataGridrows(ByVal dg As DataGrid) As Integer
        Dim strFoo As String   ' Disposable 
        Dim iCount As Integer
        Try
            For iCount = 0 To 50000
                strFoo = dg.Item(iCount, 0).ToString
            Next
        Catch ex As Exception
            Return (iCount)
        End Try
    End Function
HTH,

>Anyone know how to get the row count of a windows from datagrid without looking to it's datasource?
>I see currentrow, visiblerows but nothing for rowcount.
>
>
>I'm looking for a way to iterate through a datagrid and I don't wnat to look at the data source to know when to stop.
>
>Thanks,
    Private Sub Button1_Click(ByVal sender As System.Object, _
              ByVal e As System.EventArgs) Handles Button1.Click
        Static start_time As DateTime
        Static stop_time As DateTime
        Dim elapsed_time As TimeSpan
        start_time = Now

        Me.Text = DataGridrows(DataGrid1)
        stop_time = Now
        elapsed_time = stop_time.Subtract(start_time)

        Me.Text &= " " & elapsed_time.TotalSeconds.ToString("0.000000")
    End Sub

    Friend Function DataGridrows(ByVal dg As DataGrid) As Integer
        Dim strFoo As String
        Dim iCount As Integer
        Try
            For iCount = 0 To 50000
                strFoo = dg.Item(iCount, 0).ToString
            Next
        Catch ex As Exception
            Return (iCount)
        End Try

    End Function


    Private Sub Form1_Load(ByVal sender As System.Object, _ 
              ByVal e As System.EventArgs) Handles MyBase.Load
        BindTableToDataGrid()
    End Sub

    Public Sub BindTableToDataGrid()
        Dim dt As New DataTable
        dt.Columns.Add(New DataColumn("Item", GetType(String)))
        dt.Columns.Add(New DataColumn("Color", GetType(String)))

        For x As Integer = 1 To 25000
            dt.Rows.Add(New String() {"table", "brown"})
            dt.Rows.Add(New String() {"chair", "white"})
        Next

        DataGrid1.DataSource = dt
    End Sub
~Joe Johnston USA

"If ye love wealth better than liberty, the tranquility of servitude better than the animated contest of freedom, go home from us in peace. We ask not your counsel or arms. Crouch down and lick the hands which feed you. May your chains set lightly upon you, and may posterity forget that ye were our countrymen."
~Samuel Adams

Previous
Next
Reply
Map
View

Click here to load this message in the networking platform