Jump to content

procurar em datagrid


yozhi

Recommended Posts

Bom dia,

será que alguém me podia ajudar , como faço para procurar num datagrid usando uma textbox ?

Imports System.Data.OleDb
Public Class linhap
Dim ConnectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & Application.StartupPath & "\cbarras.mdb"
Dim consulta As String = "SELECT * FROM linhap"
Dim adaptadorconsulta As New OleDbDataAdapter(consulta, ConnectionString)
Dim MyData As New DataSet

Private impressora As String = "TSC TTP-244 Pro(CB)"
Dim fontsize As Single = 10
Dim copias As Single = 1
Dim teste As String
Dim seldata As String
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
 Dim ConnectionString As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & Application.StartupPath & "\cbarras.mdb")
 ConnectionString.Open()
 adaptadorconsulta.Fill(MyData, "linhap")
 DataGridView1.DataSource = MyData.Tables("linhap")
 txtcopias.Text = copias
 TextBox1.Text = fontsize
End Sub
Private Sub WaterMarkTextBox1_TextChanged(sender As Object, e As EventArgs)
End Sub
Private Sub DataGridView1_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick
 If e.RowIndex >= 0 Then
	 Dim row As DataGridViewRow
	 row = Me.DataGridView1.Rows(e.RowIndex)
	 seldata = row.Cells("codbarras").Value.ToString
	 teste = row.Cells("descricao").Value.ToString
	 If seldata.Length > 14 Then
		 fontsize = 8
	 Else
		 fontsize = 10
	 End If
 End If
 TextBox1.Text = fontsize
 PictureBox1.Refresh()
End Sub
Private Sub PictureBox1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles PictureBox1.Paint
 Dim stringtodraw As String = "*" & seldata & "*"
 Dim x As Integer = 0
 Dim y As Integer = 0
 Dim rec As New Rectangle(x, y, 360, 97)
 Using font As New Font("Arial", 10)
	 y = 5
	 Dim textformat As StringFormat = New StringFormat
	 textformat.Alignment = StringAlignment.Center
	 e.Graphics.DrawString(teste, font, Brushes.Black, rec, textformat)
	 Dim size As SizeF = e.Graphics.MeasureString(teste, font)
	 y += size.Height
 End Using
 Dim x1 As Integer = 0
 Dim y1 As Integer = 35
 Dim rec1 As New Rectangle(x1, y1, 360, 97)
 Dim fontpic As Single
 fontpic = fontsize + 3
 Using font As New Font("IDAutomationHC39M Free Version", fontpic)
	 Dim textformat1 As StringFormat = New StringFormat
	 textformat1.Alignment = StringAlignment.Center
	 e.Graphics.DrawString(stringtodraw, font, Brushes.Black, rec1, textformat1)
	 Dim size1 As SizeF = e.Graphics.MeasureString(stringtodraw, font)
	 y1 += Size.Height
 End Using
End Sub
Private Sub PrintDocument1_PrintPage(sender As Object, e As Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
 Dim stringtodraw As String = "*" & seldata & "*"
 Dim x As Integer = 0
 Dim y As Integer = 0
 Dim rec As New Rectangle(x, y, 196.535433071, 94.488188976)
 Using font As New Font("Arial", 10)
	 y = 5
	 Dim textformat As StringFormat = New StringFormat
	 textformat.Alignment = StringAlignment.Center
	 e.Graphics.DrawString(teste, font, Brushes.Black, rec, textformat)
	 Dim size As SizeF = e.Graphics.MeasureString(teste, font)
	 y += size.Height
 End Using
 Dim x1 As Integer = 0
 Dim y1 As Integer
 If teste = "" Then
	 y1 = 20
 Else
	 y1 = 40
 End If
 Dim rec1 As New Rectangle(x1, y1, 196.535433071, 94.488188976)
 Using font As New Font("IDAutomationHC39M Free Version", fontsize)
	 Dim textformat1 As StringFormat = New StringFormat
	 textformat1.Alignment = StringAlignment.Center
	 e.Graphics.DrawString(stringtodraw, font, Brushes.Black, rec1, textformat1)
	 Dim size1 As SizeF = e.Graphics.MeasureString(stringtodraw, font)
	 y1 += Size.Height
 End Using
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
 copias = txtcopias.Text
 PrintDocument1.PrinterSettings.PrinterName = impressora
 PrintDocument1.PrinterSettings.Copies = copias
 PrintDocument1.Print()
End Sub
Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
 fontsize = TextBox1.Text
 PictureBox1.Refresh()
End Sub
Private Sub TextBox2_TextChanged(sender As Object, e As EventArgs) Handles TextBox2.TextChanged
End Sub
End Class

cumprimentos

Link to comment
Share on other sites

Uso este código para esse fim. Espero que ajude

 //
 'Busca por coluna e texto
    Dim Texto As String = Nothing
    Try
	    If BuscaTexto.Text <> String.Empty Then
		    If IsNumeric(BuscaTexto.Text) = True Then
			    'BuscaTexto.Text = FormatCurrency(BuscaTexto.Text)
			    For i As Integer = Ultimabusca To DadosActuaisDataGridView.Rows.Count - 1
				    For Each Celula As DataGridViewCell In DadosActuaisDataGridView.Rows(i).Cells
					    'Se a coluna for a coluna 2 (Designação) então verifica o criterio
					    If Celula.ColumnIndex = 3 Or Celula.ColumnIndex = 4 Then
						    Texto = FormatCurrency(Celula.Value)
						    'Se o texto informado estiver contido na celula então selecciona toda a linha
						    If Texto.Contains(BuscaTexto.Text.ToString) And Ultimabusca <> i Then
							    Ultimabusca = i
							    Me.DadosActuaisDataGridView.Rows(Celula.RowIndex).Selected = True
							    'Faz a selecção da linha corrente
							    Me.DadosActuaisDataGridView.CurrentCell = Celula
							    Exit Sub
						    End If
					    End If
				    Next
			    Next
		    End If

		    For i As Integer = Ultimabusca To DadosActuaisDataGridView.Rows.Count - 1
			    For Each Celula As DataGridViewCell In DadosActuaisDataGridView.Rows(i).Cells
				    'Se a coluna for a coluna 2 (Designação) então verifica o criterio
				    If Celula.ColumnIndex = 2 Then
					    Texto = Celula.Value.ToString.ToLower
					    'Se o texto informado estiver contido na celula então selecciona toda a linha
					    If Texto.Contains(BuscaTexto.Text.ToLower) And Ultimabusca <> i Then
						    Ultimabusca = i
						    Me.DadosActuaisDataGridView.Rows(Celula.RowIndex).Selected = True
						    'Faz a selecção da linha corrente
						    Me.DadosActuaisDataGridView.CurrentCell = Celula
						    Exit Sub
					    End If
				    End If
			    Next
		    Next
	    Else
		    MsgBox("Caixa de Texto Vazia" & vbCrLf & "Introduza um Texto ou Valor na Cx. Texto", MsgBoxStyle.Critical, "Busca de designação")
		    BuscaTexto.Focus()
		    Exit Sub

	    End If


    Catch ex As Exception
	    ' MsgBox("Não existem mais Itens com o valor pretendido" & vbCrLf & ex.Message)
	    MsgBox("Não existem mais Itens com o valor pretendido", MsgBoxStyle.Information)
	    Ultimabusca = 0
	    BuscaTexto.Text = ""
    End Try
Link to comment
Share on other sites

Boas, eu normalmente recorro a um DataView para isso.

ex

Dim dataTable As New DataTable
'Aqui usas a tabela proveniente do fill
Dim dvProdutos As New DataView(dataTable)
Me.dgvProdutos.DataSource = dvProdutos

e no evento TextChange da TextBox

'Descricao é o nome da coluna
dvProdutos.RowFilter = "Descricao LIKE '%" & Me.txtProdutoProcurar.Text & "%'"

Mais info DataView: https://msdn.microsoft.com/en-us/library/System.Data.DataView(v=vs.110).aspx

Cumps

Link to comment
Share on other sites

mas se calhar vou usar mesmo o código do CRLF, eu tinha um que era parecido , mas acho que vou optar pelo dele.

o meu era este.

If txtpesquisar.Text <> "" Then
	 Dim s As String = txtpesquisar.Text.Trim
	 DataGridView1.ClearSelection()
	 DataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect
	 Dim algumtexto As String = s
	 Dim gridRow As Integer = 0
	 Dim gridColumn As Integer = 0
	 For Each Row As DataGridViewRow In DataGridView1.Rows
		 For Each column As DataGridViewColumn In DataGridView1.Columns
			 Dim cell As DataGridViewCell = (DataGridView1.Rows(gridRow).Cells(gridColumn))
			 If cell.Value.ToString.ToLower.Contains(algumtexto.ToLower) Then
				 'cell.Style.BackColor = Color.Yellow
				 DataGridView1.FirstDisplayedScrollingRowIndex = Row.Index
				 DataGridView1.Item(cell.ColumnIndex, cell.RowIndex).Selected = True
			 End If
			 gridColumn += 1
		 Next column
		 gridColumn = 0
		 gridRow += 1
	 Next Row
 Else
	 DataGridView1.ClearSelection()
End If

andava a tentar melhorar , para quando fosse encontrado algo que eu pesquisasse que ficasse da cor amarelo, mas só que quando apagava a cor ficava na mesma.

desde já agradeço aos dois, até porque eu sou novo nisto 😄

Edited by yozhi
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.