how to get row index and row count after set datasource on a datagridview ?

phicha

Well-known member
Joined
May 11, 2009
Messages
45
Programming Experience
Beginner
how to get row index and row count after set datasource on a datagridview ?

I am try using this code, but somehow when i on first
VB.NET:
Private Sub DataGridView1_RowStateChanged(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewRowStateChangedEventArgs) Handles DataGridView1.RowStateChanged
        'Try
        If Not CType(DataGridView1.DataSource, DataTable) Is Nothing Then
            If Not lblRecord Is Nothing Then
                If e.Row.Index > -1 Then
                    lblRecord.Text = Format(e.Row.Index + 1, "#.###") & "/" & Format(DataGridView1.Rows.Count, "#.###")
                    If Not DataGridView1.Rows(e.Row.Index) Is Nothing Then
                        txtCreatedBy.Text = DataGridView1.Rows(e.Row.Index).Cells("CreatedBy").Value
                        txtCreatedAt.Text = DataGridView1.Rows(e.Row.Index).Cells("CreatedAt").Value
                        txtModifiedBy.Text = DataGridView1.Rows(e.Row.Index).Cells("ModifiedBy").Value
                        txtModifiedAt.Text = DataGridView1.Rows(e.Row.Index).Cells("ModifiedAt").Value
                    End If
                Else
                    lblRecord.Text = "-/-"
                    txtCreatedBy.Text = ""
                    txtCreatedAt.Text = ""
                    txtModifiedBy.Text = ""
                    txtModifiedAt.Text = ""
                End If
            End If
        End If
        'Catch ex As Exception
        '    Throw ex
        'End Try
    End Sub

and how to set row index ? I try this but somehow my e.row.index still on last rows
VB.NET:
    DataGridView1.Rows.Item(IntPos).Selected = True
                    DataGridView1.CurrentCell = DataGridView1.Item(0, IntPos)  'DataGridView1.Rows.Item(IntPos).Cells(0)

thanks
 
Back
Top