Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > How to Link DataGridView control with search button and text box
 

How to Link DataGridView control with search button and text box

Hi

I have set up a datagridview control on a form that displays a .MDB file, what i now want to do is place a search button and a text box on the form so when the user types  a catalogue number into the text box, the daagridview will jump straight to that item in the database.
The database has about 3000 entries and will not be used for updating deleting etc, just read only.
My code is like this and was used in a VB6 database project. My problem in ADO.NET appears to be in  the datPrimaryRS.MoveFirst and the datPrimaryRS.Find .
A modified  code snippett would be appreciated

Regards
Rob

Option Explicit

' variables for data binding
Private datPrimaryRS As ADODB.Recordset

Private Sub Command1_Click()

Dim userin As String
    userin = Text1.Text
    If userin = "" Then
        MsgBox "Please enter a catalogue number to continue", vbExclamation, "Search"
        Else
           datPrimaryRS.MoveFirst
                datPrimaryRS.Find ("[Cat No]like '" & userin & "*'")
                Text1.Text = ""
        If datPrimaryRS.EOF Then
        MsgBox "Catalogue number not recognized, please try again.", vbExclamation, "Search Result"
       
        datPrimaryRS.MoveFirst
        Text1.Text = ""
  
    End If
    End If

Storm1  Tuesday, October 06, 2009 4:16 AM
Did you want to find in the underlying table? Or did you really want to find the item in the grid and move the selection to the row in the grid?
www.insteptech.com ; msmvps.com/blogs/deborahk
We are volunteers and ask only that if we are able to help you, that you mark our reply as your answer. THANKS!
DeborahK  Tuesday, October 06, 2009 7:03 PM
Hi Deborah

Thanks for your reply, what I want to do find the item in the grid and move the selection to the row in the grid.


Regards

Rob
Storm1  Wednesday, October 07, 2009 8:54 AM
Then you don't want to search in the dataset, you want to search in the grid.

Just loop through the rows in the grid for the column to search and exit the loop when you find what you are looking for.

Hope this helps.
www.insteptech.com ; msmvps.com/blogs/deborahk
We are volunteers and ask only that if we are able to help you, that you mark our reply as your answer. THANKS!
DeborahK  Wednesday, October 07, 2009 2:58 PM
Hi Storm1,

DeborahK's reply provides a good solution. We can call the ClearSelection method of the DataGridView at first. Then we can traverse all the rows by the Rows property of the DataGridView and check the values. If the row is what we want, set its Selected property to true.
dataGridView1.ClearSelection() 
For Each row As DataGridViewRow In dataGridView1.Rows 
    'If row is what we want, select it. 
    If CheckRow(row) Then 
        row.Selected = True 
    End If 
Next 


If the data source of the DataGridView is a BindingSource, we can call the Find method to get the index of the record we want to find. Then call the ClearSelection method of the DataGridView and set the DataGridViewRow with the same index to be selected.
Dim bindSrc As BindingSource = TryCast(dataGridView1.DataSource, BindingSource) 
Dim index As Integer = bindSrc.Find("colname", "colValue") 
dataGridView1.ClearSelection() 
dataGridView1.Rows(index).Selected = True 


You can get more about BindingSource.Find from:
http://msdn.microsoft.com/en-us/library/ms158165.aspx

Regards,
Aland Li
Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread.
Aland Li  23 hours 34 minutes ago
Aland

I dont think that this is what i am loking for, as you can see in the following VB6 cobe, i have a column called "Cat No " this column has around 3000
entries all numeric. as it holds product cataloge numbers. I simply want search this column only and find the cataloge number that the user enters into a text box and then clicks a button called search or presses the enter key.
I am using Visual basic 2008 Express.
Hope you can assist with rearanging the following code to suit VB 2008.


Regards
Rob



Option Explicit

' variables for data binding
Private datPrimaryRS As ADODB.Recordset

Private Sub Command1_Click()

Dim userin As String
    userin = Text1.Text
    If userin = "" Then
        MsgBox "Please enter a catalogue number to continue", vbExclamation, "Search"
        Else
           datPrimaryRS.MoveFirst
                datPrimaryRS.Find ("[Cat No]like '" & userin & "*'")
                Text1.Text = ""
        If datPrimaryRS.EOF Then
        MsgBox "Catalogue number not recognized, please try again.", vbExclamation, "Search Result"
       
        datPrimaryRS.MoveFirst
        Text1.Text = ""
  
    End If
    End If

Storm1  20 hours 59 minutes ago

Hi Storm1,

I am sorry that the code I provided is in C#, but what you use in VB.Net. I have modified the code snippet to be in VB.Net.

I know that my code is not exactly what you want, but the ideas and methods are similar. You can read my code again. If you cannot understand some of them, please feel free to tell me.

These are some comments of my code which can help you understand what I mean:

1.    CheckRow(row):
This is a calling to method CheckRow. You can create a method which takes a DataGridViewRow as a parameter and return a bool to indicate whether the row is what you find. In the function, you can add some code to check the cell values in the row.

2.    bindSrc.Find("colname", "colValue"):
In your case, the first parameter(‘colname’) is the column name of the field you want to search. In your case, it is “[Cat No] “. The second parameter is the value you want to search. In your case, it is the Text of the TextBox.

Regards,
Aland Li


Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread.
Aland Li  20 hours 37 minutes ago

You can use google to search for other answers

Custom Search

More Threads

• Datagrid Edit
• New to VB.NET - How can I add line numbers to DataGridView rows using VB.NET?
• Help VS Exits!!!
• About Girdview and DataList
• simple DataGridView populating and refreshing problems
• Cascaded combo boxes
• Grab current record when not using databinding
• want to learn how to write code for different controls in windows forms
• How to fill datadridview from texboxes and update access table with new records in datagridview vb.net ms access
• Update on Bound Form not working