Professional Documents
Culture Documents
Simple cdigo de ejemplo que usa el mtodo Find del componente BindingSource para
buscar un registro en un campo especfico en una tabla
formulario
Controles
Cdigo fuente
Texto planoCopiar cdigo fuenteImprimir
1. Option Explicit On
2. Option Strict On
3.
4. Imports System.Data
5. Imports System.Data.SqlClient
6.
7. Public Class Form1
8.
9.
10.
11.
12.
"Initial Catalog=la_base_de_datos"
13.
14.
15.
'Declarar un BindingSource
Private BindingSource1 As Windows.Forms.BindingSource = New Bin
dingSource
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
Try
31.
32.
33.
cn.Open()
34.
35.
36.
37.
' DataTable
38.
39.
40.
41.
da.Fill(dt)
42.
43.
44.
BindingSource1.DataSource = dt
45.
46.
47.
'''''''''''''''''''''''''''''''''''''''
48.
With DataGridView1
49.
50.
.MultiSelect = False
51.
52.
.SelectionMode = DataGridViewSelectionMode.Full
RowSelect
53.
54.
55.
.DataSource = BindingSource1.DataSource
56.
End With
57.
58.
59.
End Using
60.
' errores
61.
Catch ex As Exception
62.
63.
64.
MsgBox(ex.Message.ToString)
End Try
End Sub
65.
66.
67.
'' ''''''''''''''''''''''''''''''''''''''''''''''''''''
68.
Function Buscar( _
69.
70.
71.
72.
73.
Try
74.
75.
76.
77.
Return -1
End If
78.
79.
80.
81.
82.
83.
BindingSource.Position = fila
84.
85.
86.
Return fila
87.
88.
89.
90.
' errores
Catch ex As Exception
MsgBox(ex.Message.ToString, MsgBoxStyle.Critical)
91.
End Try
92.
93.
Return -1
94.
95.
End Function
96.
97.
98.
99.
100.
101.
102.
103.
104.
''''''''''''''''''''''''''''''''''''''''''''''''''''''
'''''''''''''''''''''
105.
106.
"Nombre", _
107.
TextBox1.Text.Trim, _
108.
BindingSource1)
109.
110.
111.
If ret = -1 Then
112.
113.
cal)
114.
Else
115.
With DataGridView1
116.
117.
.DataSource = BindingSource1
118.
' Pasarle el ndice para Visualizar la fila al
comienzo de la grilla
119.
.FirstDisplayedScrollingRowIndex = ret
120.
End With
End If
End Sub
End Class