Need help with 'Find' coding
I am trying to implement a Find button in my form.
There are 2 text boxes : one for part code and the other for part seq number and 2 Find buttons, one for each of the above.
Here is my code:
Code:
Public Class MRP_Planning
Private Sub Part_Planning_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.PartsTableAdapter.Fill(Me.PartDataSet.Parts)
End Sub
Private Sub btnFindSeqNo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFindSeqNo.Click
Dim indexNumber As Integer = Me.PartsBindingSource.Find("SequenceNumber", Me.FindBySequenceNo.Text)
Me.PartsBindingSource.Position = indexNumber
End Sub
Private Sub btnFindPartCode_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFindPartCode.Click
Dim indexNumber As Integer = Me.PartsBindingSource.Find("PartCode", Me.FindByPartCode.Text)
Me.PartsBindingSource.Position = indexNumber
End Sub
End Class
Is it possible for me to set the code where there is only 1 button and 1 textbox that the code will read the input values of part code and seq number?
Re: Need help with 'Find' coding
And then how will the app know whether to search for a part number or sequence number?
Re: Need help with 'Find' coding
Sorry my mistakes. It's 1 button and 2 textboxes. Will that still be possible?
Re: Need help with 'Find' coding
So you want to search for both at the same time, or just one TextBox will be populated at a time?
Re: Need help with 'Find' coding
If possible, I like it to search both at the same time...
Re: Need help with 'Find' coding
That's a bit tricky. The BindingSource only supports searching by one column at a time. You could search the underlying DataTable:
vb.net Code:
Dim rows As DataRow() = myDataTable.Select(String.Format("SequenceNumber = '{0}' AND PartCode = '{0}'", _
Me.FindBySequenceNo.Text, _
Me.FindByPartCode.Text), _
myBindingSource.Filter)
If rows.Length > 0 Then
For index As Integer = 0 To myBindingSource.Count - 1
If DirectCast(myBindingSource(index), DataRowView).Row = rows(0) Then
myBindingSource.Position = index
Exit For
End If
Next
End If
Re: Need help with 'Find' coding
I gotten 2 errors while inputting the above code.
1. Name 'myDataTable' is not declared
2. Operator '=' is not defined for types 'System.Data.DataRow' and 'System.Data.DataRow'
How do I rid of the errors?
Re: Need help with 'Find' coding
1. Say it to yourself: "myDataTable". What do you suppose that refers to? That code is an EXAMPLE. You have to adapt it to YOUR project, so you have to use YOUR DataTable. Don't just copy and paste blindly. Look, read and think about what the code is actually doing.
2. That one's my fault. It should be 'Is' instead of '='.
Re: Need help with 'Find' coding
datatable as in? Is it the data source that I bind it to?
Re: Need help with 'Find' coding
This:
Code:
Me.PartsTableAdapter.Fill(Me.PartDataSet.Parts)
is YOUR DataTable.