Desplazarse por un DatagridView mostrando la linea en un Form
Publicado por Miguel (281 intervenciones) el 25/10/2018 20:46:03
Tengo un formulario que con hacer click en el DataGridView me muestra los datos de la linea correspondiente.
Pero ahora he añdido cuatro botones para desplazarme, pero el caso que si primero actuamos con los botones da error pero si primero hago click en una linea y después sigo con los botones entonces funciona bien.
Puede alguien donde tengo el fallo pues no consigo verlo.
Gracias y un saludo
Pero ahora he añdido cuatro botones para desplazarme, pero el caso que si primero actuamos con los botones da error pero si primero hago click en una linea y después sigo con los botones entonces funciona bien.
Puede alguien donde tengo el fallo pues no consigo verlo.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
'Posicionarse en una fila del DatagridView para ver los datos
Private Sub Dgv_Click() Handles dgv.Click
fila = dgv.CurrentRow.Index
dgv.Rows(fila).Selected = True
Me.txtId.Text = CType(dgv.Item("Id", dgv.SelectedRows(0).Index).Value, String)
Me.txtCalle.Text = CType(dgv.Item("Calle", dgv.SelectedRows(0).Index).Value, String)
End Sub
Private Sub Primero(ByVal sender As System.Object, ByVal e As System.EventArgs)
fila = 0
cargaCalles()
MessageBox.Show("Está en el primer registro", "Aviso", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Sub
Private Sub Anterior(ByVal sender As System.Object, ByVal e As System.EventArgs)
If fila = 0 Then
Else
fila -= 1
cargaCalles()
End If
End Sub
Private Sub Siguiente(ByVal sender As System.Object, ByVal e As System.EventArgs)
Try
If fila = Obj.ds.Tables("Calles").Rows.Count - 1 Then
Else
fila += 1
cargaCalles()
End If
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Information, ToString)
End Try
End Sub
Private Sub Ultimo(ByVal sender As System.Object, ByVal e As System.EventArgs)
fila = Obj.ds.Tables("Calles").Rows.Count - 1
cargaCalles()
MessageBox.Show("Está en el último registro", "Aviso", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Sub
Sub cargaCalles()
Try
Obj.da = New MySql.Data.MySqlClient.MySqlDataAdapter("Select * from Calles", Obj.con)
Dim cb As MySql.Data.MySqlClient.MySqlCommandBuilder = New MySql.Data.MySqlClient.MySqlCommandBuilder(Obj.da)
Obj.ds = New DataSet
Obj.da.Fill(Obj.ds, "Calles")
Dim dr As DataRow
dr = Obj.ds.Tables("Calles").Rows(fila)
Me.txtId.Text = CStr(dr("Id"))
Me.txtCalle.Text = CStr(dr("Calle"))
Me.dgv.DataSource = Obj.ds.Tables("Calles")
dgv.CurrentCell = dgv.Rows(fila).Cells(0)
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Information, ToString)
End Try
End Sub
Gracias y un saludo
Valora esta pregunta
0