Chamuanza Posted January 10, 2010 at 10:46 AM Report Share #305168 Posted January 10, 2010 at 10:46 AM Boas Pessoal Como é que me posiciono em uma linha da DataGridView com o mesmo valor de Index da combobox onde seleccionei um determinado nome. Link to comment Share on other sites More sharing options...
jpaulino Posted January 10, 2010 at 10:50 AM Report Share #305170 Posted January 10, 2010 at 10:50 AM Procurando na DataGridView: Utilizando o controlo DataGridView Link to comment Share on other sites More sharing options...
Chamuanza Posted January 10, 2010 at 11:17 AM Author Report Share #305171 Posted January 10, 2010 at 11:17 AM eu estou tentando assim: Dim NumLinha As Integer = ComboBox1.SelectedIndex Me.SituacaoDataGridView.CurrentRow = NumLinha mas não funciona Link to comment Share on other sites More sharing options...
jpaulino Posted January 10, 2010 at 11:18 AM Report Share #305172 Posted January 10, 2010 at 11:18 AM Leste o artigo ? Tentaste como ele mostra ? Se não queres perder tempo e aprender como fazer, faz/tenta como queres. Link to comment Share on other sites More sharing options...
Chamuanza Posted January 10, 2010 at 11:21 AM Author Report Share #305173 Posted January 10, 2010 at 11:21 AM Ok, não percebi que estavas a direccionar-me para o artigo Link to comment Share on other sites More sharing options...
Chamuanza Posted January 10, 2010 at 11:56 AM Author Report Share #305175 Posted January 10, 2010 at 11:56 AM 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 More sharing options...
jpaulino Posted January 10, 2010 at 12:01 PM Report Share #305176 Posted January 10, 2010 at 12:01 PM E não função de pesquisa(getDataGridViewIndex) estás a pesquisar na coluna certa ? No exemplo está a pesquisar na 1ª coluna. EDIT: E não é ComboBox1.SelectedIndex, mas sim ComboBox1.SelectedValue. Cada item da combobox deve ter um campo que é mostrado e um campo que guarda um valor (ID). Link to comment Share on other sites More sharing options...
Chamuanza Posted January 10, 2010 at 12:55 PM Author Report Share #305185 Posted January 10, 2010 at 12:55 PM 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 More sharing options...
jpaulino Posted January 10, 2010 at 01:05 PM Report Share #305187 Posted January 10, 2010 at 01:05 PM Verifica no inicio do método SelectedIndexChanged se ele é diferente de -1 Link to comment Share on other sites More sharing options...
Chamuanza Posted January 10, 2010 at 01:34 PM Author Report Share #305193 Posted January 10, 2010 at 01:34 PM :nono1: eu fazia se soubesse com Link to comment Share on other sites More sharing options...
jpaulino Posted January 10, 2010 at 02:09 PM Report Share #305197 Posted January 10, 2010 at 02:09 PM Deve ser +/- isto (não testado) If ComboBox2.SelectedIndex = -1 Then Exit Sub Link to comment Share on other sites More sharing options...
Chamuanza Posted January 10, 2010 at 02:20 PM Author Report Share #305199 Posted January 10, 2010 at 02:20 PM 😁 RESOLVIDO A funcionar bem - Obrigado pela atenção Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now