POSSO MUDAR POSI?ÃO DE UM ITEM NO DATAGRIDVIEW?
Bom dia,Colegas!
Minha dúvida é simples:
Tenho um objeto DataGridWiew , no meu projeto VB.NET.
No Grid, tenho 10 colunas, e cerca de 15 linhas.
Ocorre que em determinadas situações, eu preciso fazer com que ,por exemplo , a sétima linha
do Grid, passe a ser a terceira do Grid. Ou seja,mude de posição.
Antes,de eu criar um código para isto, gostaria de saber se já existe um método pronto no objeto, que permita
realizar esta mudança.
Agradeço qualquer orientação
Minha dúvida é simples:
Tenho um objeto DataGridWiew , no meu projeto VB.NET.
No Grid, tenho 10 colunas, e cerca de 15 linhas.
Ocorre que em determinadas situações, eu preciso fazer com que ,por exemplo , a sétima linha
do Grid, passe a ser a terceira do Grid. Ou seja,mude de posição.
Antes,de eu criar um código para isto, gostaria de saber se já existe um método pronto no objeto, que permita
realizar esta mudança.
Agradeço qualquer orientação
até onde vai meu conhecimento, nunca ouvi falar que fosse possÃvel fazer, mas voce pode carregar o grid em rotinas diferentes com as linhas na ordem que voce quiser, aÃ, de acordo com a necessidade, voce recarrega o grid com a rotina mantgendo as linhas na ordem que voce quer
Eu bolei algo assim e que funciona.
[ô]botão que move a linha para cima
Private Sub BTN_UP_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BTN_UP.Click
If GRID_LISTA.Rows.Count < 1 Then Exit Sub
Me.MoveLinha(Me.GRID_LISTA.CurrentRow, GRID_LISTA.CurrentRow.Index - 1)
End Sub
[ô]botão que move a linha para baixo
Private Sub BTN_DOW_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BTN_DOW.Click
If GRID_LISTA.Rows.Count < 1 Then Exit Sub
Me.MoveLinha(Me.GRID_LISTA.CurrentRow, GRID_LISTA.CurrentRow.Index + 1)
End Sub
Private Sub MoveLinha(ByVal pLinha As DataGridViewRow, ByVal pPosicao As Integer)
Try
If pPosicao < 0 Then Exit Sub
If pPosicao > GRID_LISTA.Rows.Count - 1 Then Exit Sub
Me.GRID_LISTA.Rows.Remove(pLinha)
Me.GRID_LISTA.Rows.Insert(pPosicao, pLinha)
My.Application.DoEvents()
Me.GRID_LISTA.Focus()
Me.GRID_LISTA.CurrentCell = GRID_LISTA.Rows(pPosicao).Cells(2)
Me.GRID_LISTA.Rows(pPosicao).Selected = True
[ô]*acerta os itens
Dim i As Integer
For i = 0 To GRID_LISTA.Rows.Count - 1
GRID_LISTA.Rows(i).Cells(GLITEM.Name).Value = i + 1
Next
Catch
End Try
End Sub
Selecione a linha que vc quer movimentar e movimente para cima ou para baixo.
[ô]botão que move a linha para cima
Private Sub BTN_UP_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BTN_UP.Click
If GRID_LISTA.Rows.Count < 1 Then Exit Sub
Me.MoveLinha(Me.GRID_LISTA.CurrentRow, GRID_LISTA.CurrentRow.Index - 1)
End Sub
[ô]botão que move a linha para baixo
Private Sub BTN_DOW_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BTN_DOW.Click
If GRID_LISTA.Rows.Count < 1 Then Exit Sub
Me.MoveLinha(Me.GRID_LISTA.CurrentRow, GRID_LISTA.CurrentRow.Index + 1)
End Sub
Private Sub MoveLinha(ByVal pLinha As DataGridViewRow, ByVal pPosicao As Integer)
Try
If pPosicao < 0 Then Exit Sub
If pPosicao > GRID_LISTA.Rows.Count - 1 Then Exit Sub
Me.GRID_LISTA.Rows.Remove(pLinha)
Me.GRID_LISTA.Rows.Insert(pPosicao, pLinha)
My.Application.DoEvents()
Me.GRID_LISTA.Focus()
Me.GRID_LISTA.CurrentCell = GRID_LISTA.Rows(pPosicao).Cells(2)
Me.GRID_LISTA.Rows(pPosicao).Selected = True
[ô]*acerta os itens
Dim i As Integer
For i = 0 To GRID_LISTA.Rows.Count - 1
GRID_LISTA.Rows(i).Cells(GLITEM.Name).Value = i + 1
Next
Catch
End Try
End Sub
Selecione a linha que vc quer movimentar e movimente para cima ou para baixo.
Se você está preenchendo seu grid com um List<T> basta adicionar uma propriedade [Ô]Ãndice[Ô] na entidade e ordenar como quiser atribuindo o Ãndice que quiser e usando o método de extensão OrderBy ou OrderByDescending.
Obrigado,Pessoal !
Tópico encerrado , respostas não são mais permitidas