Jump to content

[Resolvido] Percorrer dataGridView linha a linha através de botão


Joca
 Share

Recommended Posts

Boa noite,

Eu consigo percorrer as linhas de uma DataGridView, desde o início, fazendo algo tal como o seguinte. Em que:

- "btnSeguinte" é um botão;

- "dgvExibeDados" é a DataGridView;

- e, o "ds" é a dataset obtida via OleDb.

Private Sub btnSeguinte_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSeguinte.Click

        Dim _indiceColuna As Integer = dgvExibeDados.CurrentCell.ColumnIndex
        Dim _indiceLinha As Integer = dgvExibeDados.CurrentCell.RowIndex 
        Dim _TotalLinhas As Integer
        ' Dim _i As Integer = -1 <- declarado no inicio 

        _TotalLinhas = ds.Tables("TESTE").Rows.Count

        _i = _i + 1
        txtDados1.Text = ds.Tables("TESTE").Rows(_indiceLinha + _i).Item(_indiceColuna)

end sub

A minha questão é: como é que faço para reiniciar a contagem da variável "_i" se eu seleccionar uma outra linha na datagridview?

Pensei em usar também a seguinte opção, mas não estou a ter muito sucesso....  😛 Alguma ideia?

Dim dr As DataGridViewRow = dgvExibeDados.CurrentRow

txtDados1.Text = dgvExibeDados.Rows.GetNextRow(dr.Index, DataGridViewElementStates.Visible)
Link to comment
Share on other sites

=) eu gosto de ajudar pessoas 👍

  Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim i As Integer = DataGridView1.CurrentCell.RowIndex
        DataGridView1.Rows(i).Selected = False
        DataGridView1.CurrentCell = DataGridView1.Rows(i + 1).Cells(0)
        DataGridView1.Rows(i + 1).Selected = True
        Label1.Text = DataGridView1.Rows(DataGridView1.CurrentCell.RowIndex).Cells(0).Value
    End Sub

No Form Load apos o carregamento da grid meti isto:

      Label1.Text = DataGridView1.Rows(DataGridView1.CurrentCell.RowIndex).Cells(0).Value

também meti o select mode como fullrow mas isso não interessa muito =)

Link to comment
Share on other sites

Graças à tua ajuda, percebi onde estava a minha asneira. Readaptei o teu código para ficar assim no evento click do botão:

Dim _indiceColuna As Integer = dgvExibeDados.CurrentCell.ColumnIndex
        Dim _indiceLinha As Integer = dgvExibeDados.CurrentCell.RowIndex
        Dim _TotalLinhas As Integer
        Dim _i As Integer = dgvExibeDados.CurrentCell.RowIndex

        _TotalLinhas = dgvExibeDados.Rows.Count

        dgvExibeDados.CurrentCell = dgvExibeDados.Rows(_i + 1).Cells(_indiceColuna)
        txtDados1.Text = dgvExibeDados.Rows(dgvExibeDados.CurrentCell.RowIndex).Cells(_indiceColuna).Value

E, alterei o código do evento click da dgvExibeDados (a DatagridView) para actualizar de imediato a Textbox:

Dim _indiceColuna As Integer = dgvExibeDados.CurrentCell.ColumnIndex
        Dim _indiceLinha As Integer = dgvExibeDados.CurrentCell.RowIndex
        Dim _TotalLinhas As Integer
        Dim _i As Integer = dgvExibeDados.CurrentCell.RowIndex

        _TotalLinhas = dgvExibeDados.Rows.Count

       txtDados1.Text = dgvExibeDados.Rows(_i).Cells(_indiceColuna).Value

Obviamente, que tem código repetido e, talvez a ser alterado (tal como o uso do selected que indicaste... ainda em estudo) mas, para testes vai servir para já. Agora já posso trabalhar descansado na "captura" dos erros usando a variável "_Total de linhas" entre outras futuras variáveis.

Obrigado pela dica! 👍

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.