E TAPAS DE LA P LANEACIÓN I Y II Clase 6 Clase 7 Clase 8 Clase 9 Clase 10.
Clase 231014
-
Upload
roni-aguilar-huaccha -
Category
Documents
-
view
215 -
download
3
description
Transcript of Clase 231014
INSTITUTO DE EDUCACIN SUPERIOR PBLICOComputacin e InformticaDAVID SNCHEZ INFANTEUnidad Didctica: Programacin Distribuida
DISEAR EL SIGUIENTE FORMULARIO:ESCRIBIR EL SIGUIENTE CDIGO FUENTE:Imports System.Data.SqlClient
Public Class Form2 ' para filtrar con el operador Like Enum e_FILTER_OPTION SIN_FILTRO = 0 CADENA_QUE_COMIENCE_CON = 1 CADENA_QUE_NO_COMIENCE_CON = 2 CADENA_QUE_CONTENGA = 3 CADENA_QUE_NO_CONTENGA = 4 CADENA_IGUAL = 5 End Enum
' cadena de conexin para SQL EXPRESS en modo local Private Const cs As String = "Data Source=CENCACI\DSI;" & "Integrated Security=True;" & "Initial Catalog=BD_Prueba" 'Instanciar el componente BindingSource Private BindingSource1 As Windows.Forms.BindingSource = New BindingSource
Private Sub Aplicar_Filtro() ' filtrar por el campo nombre Dim ret As Integer = Filtrar_DataGridView("nombre", TextBox1.Text.Trim, BindingSource1, DataGridView1, CType(ComboBox1.SelectedIndex, e_FILTER_OPTION)) If ret = 0 Then ' si no hay registros cambiar el color del txtbox TextBox1.BackColor = Color.Red Else TextBox1.BackColor = Color.White End If ' visualizar la cantidad de registros Me.Text = ret & " Registros encontrados" End Sub Function Filtrar_DataGridView(ByVal Columna As String, ByVal texto As String, ByVal BindingSource As BindingSource, ByVal DataGridView As DataGridView, Optional ByVal Opcion_Filtro As e_FILTER_OPTION = Nothing) As Integer ' verificar que el DataSource no est vacio If BindingSource1.DataSource Is Nothing Then Return 0 End IfEnd Class Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load Try ' Inicializar la conexin y abrir Using cn As SqlConnection = New SqlConnection(cs) cn.Open() ' Inicializar DataAdapter indicando el sql para recuperar 'los registros de la tabla Dim da As New SqlDataAdapter("SELECT * FROM tutorial", cn) Dim dt As New DataTable ' crear un DataTable ' llenarlo da.Fill(dt) ' enlazar el DataTable al BindingSource BindingSource1.DataSource = dt ' agregar las opciones al combobox With (ComboBox1) 'cargar los items de opciones para filtrar .Items.Add("No filtrar") .Items.Add("Que comience con") .Items.Add("Que No comience con") .Items.Add("Que contenga") .Items.Add("Que No contenga") .Items.Add("Que sea igual") .DropDownStyle = ComboBoxStyle.DropDownList .SelectedIndex = 1 End With End Using ' errores Catch ex As Exception MsgBox(ex.Message.ToString) End Try End Sub
Try Dim filtro As String = String.Empty ' Seleccionar la opcin Select Case Opcion_Filtro Case e_FILTER_OPTION.CADENA_QUE_COMIENCE_CON filtro = "like '" & texto.Trim & "%'" Case e_FILTER_OPTION.CADENA_QUE_NO_COMIENCE_CON filtro = "Not like '" & texto.Trim & "%'" Case e_FILTER_OPTION.CADENA_QUE_NO_CONTENGA filtro = "Not like '%" & texto.Trim & "%'" Case e_FILTER_OPTION.CADENA_QUE_CONTENGA filtro = "like '%" & texto.Trim & "%'" Case e_FILTER_OPTION.CADENA_IGUAL filtro = "='" & texto.Trim & "'" End Select ' Opcin para no filtrar If Opcion_Filtro = e_FILTER_OPTION.SIN_FILTRO Then filtro = String.Empty End If ' armar el sql If filtro String.Empty Then filtro = "[" & Columna & "]" & filtro End If ' asigar el criterio a la propiedad Filter del BindingSource BindingSource.Filter = filtro ' enlzar el datagridview al BindingSource DataGridView.DataSource = BindingSource.DataSource ' retornar la cantidad de registros encontrados Return BindingSource.Count ' errores Catch ex As Exception MsgBox(ex.Message.ToString, MsgBoxStyle.Critical) End Try Return 0 End Function
Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged Aplicar_Filtro() End Sub
Prof. Roni Aguilar Huaccha3San Pedro de Lloc