terça-feira, 17 de outubro de 2017

Navegar entre DataGridView - VB.NET

Para navegar entre linhas do DataGridView é muito simples.

Adicione uma Variavel Global;


1:  Dim WTLinha As Integer

Agora no botão Próximo Registro, adicione:


1:  If WTLinha = Nothing Then  
2:    WTLinha = DataGridView1.SelectedCells(0).RowIndex ' Aqui pegamos qual linha o usuário selecionou  
3:    WTLinha += 1  
4:    DataGridView1.CurrentCell = DataGridView1.Rows(WTLinha).Cells(0) ' Aqui jogamos para proxima linha  
5:  Else  
6:   WTLinha += 1  
7:    DataGridView1.CurrentCell = DataGridView1.Rows(WTLinha).Cells(0) ' Aqui jogamos para proxima linha  
8:  End If  

No botão Voltar é só mudar WTLinha += 1 Para WTLinha -= 1

Para selecionar o primeiro registro;


1:   DataGridView1.CurrentCell = DataGridView1.Rows(0).Cells(0)   

Para selecionar o ultimo registro;


1:   Dim Count As Integer = DataGridView1.Rows.Count -1  
2:   DataGridView1.CurrentCell = DataGridView1.Rows(Count).Cells(0)   

Este método acima é o que eu desenvolvi por conta, porém existe esse outro;




1:  'Proximo  
2:  Try  
3:        Dim lastRowNum As Integer = (Me.DataGridView1.Rows.Count - 1 - 1)  
4:        Dim curDataGridViewRow As DataGridViewRow = DataGridView1.CurrentRow  
5:        Dim curRowNum As Integer = curDataGridViewRow.Index  
6:        If (curRowNum >= lastRowNum) Then  
7:        Else  
8:          Dim nextRow As DataGridViewRow = DataGridView1.Rows(curRowNum + 1)  
9:          DataGridView1.CurrentCell = nextRow.Cells(0)  
10:          nextRow.Selected = True  
11:        End If  
12:      Catch  
13:      End Try  
14:  'Voltar  
15:  Dim lastRowNum As Integer = (Me.DataGridView1.Rows.Count + 1 + 1)  
16:      Dim curDataGridViewRow As DataGridViewRow = DataGridView1.CurrentRow  
17:      Dim curRowNum As Integer = curDataGridViewRow.Index  
18:      If (curRowNum >= lastRowNum) Then  
19:      Else  
20:        Dim nextRow As DataGridViewRow = DataGridView1.Rows(curRowNum - 1)  
21:        DataGridView1.CurrentCell = nextRow.Cells(0)  
22:        nextRow.Selected = True  
23:      End If  


Bom proveito, Abrçs...

COMENTE

& Compartilhe

2 comentários:

 

Copyright © 2015 Projeto Programação

Distributed By My Blogger Themes | Designed by Templateism