Jump to content

[Resolvido] Botão procurar usando DataGridView


danielafnovo

Recommended Posts

Imports System.Data.OleDb
Imports System.Data
Imports System.Data.SqlClient

Public Class potencial

Dim cn As New OleDb.OleDbConnection
Dim Da As New OleDbDataAdapter
Dim Dt As DataTable
Dim Cmd As New OleDbCommand
Private connectionstring As String
Dim Sql As String

Private Sub potencial_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
	'TODO: This line of code loads data into the 'Base_de_DadosDataSet.Histórico' table. You can move, or remove it, as needed.
	Me.HistóricoTableAdapter.Fill(Me.Base_de_DadosDataSet.Histórico)

	CarregaDados()

End Sub


Private Sub CarregaDados()

	cn.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Ana Alves\Documents\Base de Dados.accdb"
	cn.Open()

	Try
		With Cmd
			.CommandType = CommandType.Text
			.CommandText = "SELECT * from Histórico"
			.Connection = cn
		End With

		With Da
			.SelectCommand = Cmd
			Dt = New DataTable
			.Fill(Dt)
			DataGridView1.DataSource = Dt
		End With
	Catch ex As Exception
		MsgBox(ex.Message)
	End Try

	cn.Close()

End Sub


Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
	If TextBox1.Text = String.Empty Then
		MsgBox("Digite o potencial que quer procurar")
	Else
		Procurar(Me.TextBox1.Text)
	End If
End Sub

Private Sub Procurar(ByVal aa As Integer)
	cn.Close()
	connectionstring = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Ana Alves\Documents\Base de Dados.accdb"
	cn.ConnectionString = connectionstring
	cn.Open()
	Dim dt As DataTable = New DataTable("Histórico")

	' Dim Da As New OleDb.OleDbDataAdapter("SELECT Potencial_Negócio, Empresa FROM Histórico " & _
	'															  " WHERE Potencial_Negócio=" & aa, cn)

	Me.HistóricoBindingSource.Filter = "'Potencial Negócio' LIKE'%" & Me.TextBox1.Text & "%'"

	Sql = "Select * From Histórico "
	Da = New OleDb.OleDbDataAdapter(Sql, cn)
	DataGridView1.DataSource = HistóricoBindingSource
	Da.Fill(dt)
	cn.Close()
	CarregaDados()



End Sub

Private Sub DataGridView1_CellContentClick(sender As System.Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick
	Dim Potencial_Negócio As String = DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex).Value()
	'Dim Potencial_Negócio As Integer = DataGridView1.Rows(e.RowIndex).Cells(0).Value()
	Procurar(Potencial_Negócio)
End Sub
End Class

O programa é assim :

tumblr_mnv5ygtYYT1rv5q6ko1_500.png

O que eu queria era que ao digitar um valor na TextBox e ao clicar em procurar aparecesse só as empresas com esse valor...No entanto, ao executar não faz nada... Aguardo resposta o mais rápido possível, por favor, é importante... Obrigado...

Edited by ribeiro55
Link to comment
Share on other sites

É disto que estás a falar ?

Private Sub Button1_KeyPress(sender As Object, e As System.Windows.Forms.KeyPressEventArgs) Handles Button1.KeyPress

CarregaDados()

End SubPrivate Sub Button1_KeyPress(sender As Object, e As System.Windows.Forms.KeyPressEventArgs) Handles Button1.KeyPress

CarregaDados()

End Sub

Comecei há pouco tempo e não percebo muito disto, eu pus isso mas dá-me um erro ao ligar o programa, ao clicar no form para ir como ta na imagem diz que não foi fornecido um ou mais valores para os parametros necessarios...

Link to comment
Share on other sites

  • 7 months later...
Imports System.Data.OleDb
Imports System.Data
Imports System.Data.SqlClient

Public Class potencial

Dim cn As New OleDb.OleDbConnection
Dim Da As New OleDbDataAdapter
Dim Dt As DataTable
Dim Cmd As New OleDbCommand
Private connectionstring As String
Dim Sql As String

Private Sub potencial_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
	'TODO: This line of code loads data into the 'Base_de_DadosDataSet.Histórico' table. You can move, or remove it, as needed.
	Me.HistóricoTableAdapter.Fill(Me.Base_de_DadosDataSet.Histórico)

	CarregaDados()

End Sub


Private Sub CarregaDados()

	cn.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Ana Alves\Documents\Base de Dados.accdb"
	cn.Open()

	Try
		With Cmd
			.CommandType = CommandType.Text
			.CommandText = "SELECT * from Histórico"
			.Connection = cn
		End With

		With Da
			.SelectCommand = Cmd
			Dt = New DataTable
			.Fill(Dt)
			DataGridView1.DataSource = Dt
		End With
	Catch ex As Exception
		MsgBox(ex.Message)
	End Try

	cn.Close()

End Sub


Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
	If TextBox1.Text = String.Empty Then
		MsgBox("Digite o potencial que quer procurar")
	Else
		Procurar(Me.TextBox1.Text)
	End If
End Sub

Private Sub Procurar(ByVal aa As Integer)
	cn.Close()
	connectionstring = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Ana Alves\Documents\Base de Dados.accdb"
	cn.ConnectionString = connectionstring
	cn.Open()
	Dim dt As DataTable = New DataTable("Histórico")

	' Dim Da As New OleDb.OleDbDataAdapter("SELECT Potencial_Negócio, Empresa FROM Histórico " & _
	'															  " WHERE Potencial_Negócio=" & aa, cn)

	Me.HistóricoBindingSource.Filter = "'Potencial Negócio' LIKE'%" & Me.TextBox1.Text & "%'"

	Sql = "Select * From Histórico "
	Da = New OleDb.OleDbDataAdapter(Sql, cn)
	DataGridView1.DataSource = HistóricoBindingSource
	Da.Fill(dt)
	cn.Close()
	CarregaDados()



End Sub

Private Sub DataGridView1_CellContentClick(sender As System.Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick
	Dim Potencial_Negócio As String = DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex).Value()
	'Dim Potencial_Negócio As Integer = DataGridView1.Rows(e.RowIndex).Cells(0).Value()
	Procurar(Potencial_Negócio)
End Sub
End Class

O programa é assim :

tumblr_mnv5ygtYYT1rv5q6ko1_500.png

O que eu queria era que ao digitar um valor na TextBox e ao clicar em procurar aparecesse só as empresas com esse valor...No entanto, ao executar não faz nada... Aguardo resposta o mais rápido possível, por favor, é importante... Obrigado...

Boas, podes me dizer como declaras o teu Me.HistóricoBindingSource.Filter?

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.