Filtro un ListView y retorna un indice > que reg. tengo en el DatagridView
Publicado por Miguel (281 intervenciones) el 18/04/2016 13:28:37
Adjunto el formulario para facilitar la explicación.
En el formulario verán un DataGridView y superpuesto un ListView( visible=false).
1.- Tengo un procedimiento Sub que al hacer click en el DataGridview sobre una linea la deja señalizada y ademas los valores de esa linea se colocan en los textBox de cabecera.
2.- Cuando quiero filtar los datos hago click en textBuscar, se pone el Listview (visible=true) y conforme tecleo un carácter me saca los datos en el LisView.
3.- Para seleccionar la linea que quiero hago click sobre el ListView, entonces con el primer campo (IdProv) hago una busqueda con é,l para posicionarme en la linea correspondiente del DataGridView, previamente el ListView lo pongo (visible=false).
Hasta este punto todo me funciona correctamente, pero cuando capturo el indice del registro en el DataGridView resulta que me retorna un indice=94 cuando solo existen 52 registros en dicha tabla.
Después de un par de días dando vueltas no he conseguido ver el problema, así que agradezco cualquier ayuda u orientación para saber donde mirar.
Gracias y un saludo
En el formulario verán un DataGridView y superpuesto un ListView( visible=false).
1.- Tengo un procedimiento Sub que al hacer click en el DataGridview sobre una linea la deja señalizada y ademas los valores de esa linea se colocan en los textBox de cabecera.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
Private Sub dgv_Click(sender As Object, e As System.EventArgs) Handles dgv.Click
Try
presentaFila(sender, e)
Catch ex As Exception
MessageBox.Show(ex.Message.ToString)
End Try
End Sub
Private Sub presentaFila(sender As Object, e As System.EventArgs)
Try
Dim row As DataGridViewRow = Me.dgv.CurrentRow
Dim num As Integer = Me.dgv.ColumnCount
objProv.fila = Me.dgv.CurrentRow.Index
Me.txtIdProv.Text = CStr(row.Cells(num - 2).Value)
Me.txtProvincia.Text = Convert.ToString(CStr(row.Cells(num - 1).Value))
objProv.cargaPropertis(objProv.fila)
Catch ex As Exception
MessageBox.Show(ex.Message.ToString)
End Try
End Sub
2.- Cuando quiero filtar los datos hago click en textBuscar, se pone el Listview (visible=true) y conforme tecleo un carácter me saca los datos en el LisView.
3.- Para seleccionar la linea que quiero hago click sobre el ListView, entonces con el primer campo (IdProv) hago una busqueda con é,l para posicionarme en la linea correspondiente del DataGridView, previamente el ListView lo pongo (visible=false).
Hasta este punto todo me funciona correctamente, pero cuando capturo el indice del registro en el DataGridView resulta que me retorna un indice=94 cuando solo existen 52 registros en dicha tabla.
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
Private Sub txtBuscar_TextChanged(ByVal sender As Object, ByVal e As EventArgs) Handles txtBuscar.TextChanged
Try
If iniciando Then Exit Sub
Dim dt As DataTable
If Not BuscarDatos(dt, txtBuscar.Text) Then Throw New Exception("Sucedio un error al consultar los datos.")
If Not RellenarListview(dt) Then Throw New Exception("Sucedio un error al rellenar el listado.")
Catch ex As Exception
MsgBox(ex.Message, , "Error")
End Try
End Sub
Function BuscarDatos(ByRef dt As DataTable, Optional ByVal filtro As String = "") As Boolean
Dim bolResultado As Boolean = True
Dim cmd As OleDbCommand
Try
If dt Is Nothing Then dt = New DataTable
cmd = New OleDbCommand("SELECT IdProv,Provincia From Provincias WHERE Provincia LIKE @FILTRO", cn)
cmd.Parameters.AddWithValue("@FILTRO", "" & filtro & "%")
Using DA As New OleDbDataAdapter(cmd)
DA.Fill(dt)
End Using
Catch ex As Exception
bolResultado = False
End Try
Return bolResultado
End Function
Function RellenarListview(ByVal dt As DataTable) As Boolean
Dim bolResultado As Boolean = True
Dim lstElemento As ListViewItem
Try
Me.listaDatos.Items.Clear()
Me.listaDatos.Columns.Clear()
For Each col As DataColumn In dt.Columns
listaDatos.Columns.Add(col.ColumnName, col.ColumnName)
Next
For Each row As DataRow In dt.Rows
lstElemento = New ListViewItem
lstElemento.Text = row(0).ToString()
For intcontador As Integer = 1 To dt.Columns.Count - 1
lstElemento.SubItems.Add(row(intcontador).ToString())
Next
listaDatos.Items.Add(lstElemento)
Next
Me.listaDatos.AutoResizeColumns(ColumnHeaderAutoResizeStyle.HeaderSize)
Catch ex As Exception
bolResultado = False
End Try
Return bolResultado
End Function
Private Sub ListDatos_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles listaDatos.SelectedIndexChanged
Try
If iniciando Then Exit Sub
Dim campo As String = Trim(listaDatos.SelectedItems.Item(0).Text)
Me.listaDatos.Visible = False
Dim da = New OleDbDataAdapter("Select * From Provincias", cn)
da.Fill(ds, "Provincias")
With ds.Tables("Provincias")
.DefaultView.Sort = "IdProv"
dgv.DataSource = .DefaultView
End With
objProv.fila = ds.Tables("Provincias").DefaultView.Find(campo)
If objProv.fila <> -1 Then
With dgv
.FirstDisplayedScrollingRowIndex = objProv.fila
.CurrentCell = .Rows(objProv.fila).Cells(0)
.Select()
End With
presentaFila(sender, e)
Else
MsgBox("no se encontro el dato buscado en el DataGridView")
End If
Catch ex As Exception
MessageBox.Show(ex.Message.ToString)
End Try
End Sub
Después de un par de días dando vueltas no he conseguido ver el problema, así que agradezco cualquier ayuda u orientación para saber donde mirar.
Gracias y un saludo
- Formulario.zip(133,9 KB)
Valora esta pregunta


0