Jump to content

Posicionamento em Linha da DataGridView


Chamuanza
 Share

Recommended Posts

Olá Paulino

Seguindo o raciocinio do artigo coloquei a função

 ' Função que pesquisa na coluna 0 por um id e devolve a posição na lista 
    Private Function getDataGridViewIndex(ByVal id As Integer) As Integer

        For Each row As DataGridViewRow In Me.SituacaoDataGridView.Rows

            If Not row.IsNewRow Then
                If row.Cells(0).Value = id Then
                    Return row.Index
                End If
            End If

        Next

        Return 0

    End Function

e no sub coloquei

Private Sub ComboBox2_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ComboBox2.SelectedIndexChanged
        'faz a sincronização de dados entre as combobox
        ComboBox1.SelectedIndex = ComboBox2.SelectedIndex

        Dim NumLinha As Integer = ComboBox1.SelectedIndex

        Dim result As Integer = getDataGridViewIndex(NumLinha)


        With Me.SituacaoDataGridView

            ' Define que a selecção será efectuada na linha toda 
            .SelectionMode = DataGridViewSelectionMode.FullRowSelect

            ' Define que a célula/linha actual é a da pesquisa 
            .CurrentCell = SituacaoDataGridView.Rows(result).Cells(0)

            ' Define que a primeira linha a aparecer visível no topo da 
            '  DataGridView é a encontrada  (caso a lista seja grande) 
            .FirstDisplayedScrollingRowIndex = result

        End With


    End Sub

mas o result final retorna 0

Link to comment
Share on other sites

Paulino já está a funcionar, com esta alteração

Private Sub ComboBox2_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ComboBox2.SelectedIndexChanged
        'faz a sincronização de dados entre as combobox
        ComboBox1.SelectedIndex = ComboBox2.SelectedIndex


        Dim NumLinha As Integer = SituacaoDataGridView.Item(0, ComboBox1.SelectedIndex).Value
        Dim result As Integer = getDataGridViewIndex(NumLinha)


        With Me.SituacaoDataGridView

            ' Define que a selecção será efectuada na linha toda 
            .SelectionMode = DataGridViewSelectionMode.FullRowSelect

            ' Define que a célula/linha actual é a da pesquisa 
            .CurrentCell = SituacaoDataGridView.Rows(result).Cells(0)

            ' Define que a primeira linha a aparecer visível no topo da 
            '  DataGridView é a encontrada  (caso a lista seja grande) 
            '.FirstDisplayedScrollingRowIndex = result

        End With


    End Sub

,mas quando saio do form da erro na linha marcada a amarelo

Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.