Ir para conteúdo

POWERED BY:

Arquivado

Este tópico foi arquivado e está fechado para novas respostas.

borges02

Trabalhando com DataGridView!

Recommended Posts

Bom dia!

 

Alguem poderia me ajudar a converter esse código de VB para C#. Tentei converter, mas na hora de compilar, aparece alguns erros de código. Esse código é sobre teclas de atalho para movimentar as linhas de um datagridview. Igual o mecanismo da barra de rolagem, mas usando teclas. PageUp, PageDown, Top, Up...

 

Private Sub swapRows(ByVal range As mode)
			Dim iSelectedRow As Integer = -1
			For iTmp As Integer = 0 To Datagridview.Rows.Count - 1
				If Datagridview.Rows(iTmp).Selected Then
					iSelectedRow = iTmp
					Exit For
				End If
			Next

			If iSelectedRow <> -1 Then
				Dim sTmp(4) As String
				For iTmp As Integer = 0 To Datagridview.Columns.Count - 1
					sTmp(iTmp) = Datagridview.Rows(iSelectedRow).Cells(iTmp).Value.ToString
				Next
				
				Dim iNewRow As Integer
				If range = mode.down Then
					iNewRow = iSelectedRow + 1
				ElseIf range = mode.up Then
					iNewRow = iSelectedRow - 1
				End If
				
				If range = mode.up Or range = mode.down Then
					For iTmp As Integer = 0 To Datagridview.Columns.Count - 1
						Datagridview.Rows(iSelectedRow).Cells(iTmp).Value = Datagridview.Rows(iNewRow).Cells(iTmp).Value
						Datagridview.Rows(iNewRow).Cells(iTmp).Value = sTmp(iTmp)
					Next
					toSelect(iNewRow)
				ElseIf range = mode.top Or range = mode.bottom Then
					reshuffleRows(sTmp, iSelectedRow, range)
				End If
			End If
	End Sub


 Private Sub toSelect(ByVal iNewRow As Integer)
		Datagridview.Rows(iNewRow).Selected = True
	End Sub



Private Sub reshuffleRows(ByVal sTmp() As String, ByVal iSelectedRow As Integer, ByVal Range As mode)
		If Range = mode.top Then
			Dim iFirstRow As Integer = 0
			If iSelectedRow > iFirstRow Then
				For iTmp As Integer = iSelectedRow To 1 Step -1
					For iCol As Integer = 0 To Datagridview.Columns.Count - 1
						Datagridview.Rows(iTmp).Cells(iCol).Value = Datagridview.Rows(iTmp - 1).Cells(iCol).Value
					Next
				Next
				For iCol As Integer = 0 To Datagridview.Columns.Count - 1
					Datagridview.Rows(iFirstRow).Cells(iCol).Value = sTmp(iCol).ToString
				Next
				toSelect(iFirstRow)
			End If
		Else
			Dim iLastRow As Integer = Datagridview.Rows.Count - 1
			If iSelectedRow < iLastRow Then
				For iTmp As Integer = iSelectedRow To iLastRow - 1
					For iCol As Integer = 0 To Datagridview.Columns.Count - 1
						Datagridview.Rows(iTmp).Cells(iCol).Value = Datagridview.Rows(iTmp + 1).Cells(iCol).Value
					Next
				Next
				For iCol As Integer = 0 To Datagridview.Columns.Count - 1
					Datagridview.Rows(iLastRow).Cells(iCol).Value = sTmp(iCol).ToString
				Next
				toSelect(iLastRow)
			End If
		End If
	End Sub
Desde já agradeço pela ajuda e atenção...

Muito Obrigado...

Compartilhar este post


Link para o post
Compartilhar em outros sites

Borges vou te responder o mesmo que coloquei no tópico do cara do Delphi: http://forum.imasters.com.br/index.php?showtopic=287792

Seu projeto é web ou windows?

E acho que ninguém vai ter tempo pra converter sua função inteira, poste o que já começou a fazer, e erros que estão acontecendo ou dúvidas específicas sobre parte do código.

 

Abraços...

Compartilhar este post


Link para o post
Compartilhar em outros sites

×

Informação importante

Ao usar o fórum, você concorda com nossos Termos e condições.