Filling TextBox with search results linked to DataGridView

In Visual Basic 2005 Express I am working on a simple search application to search a SQL database table (FullDocuments) using TextBox1 and Button1 to enter search criteria and DataGridView1 and TextBox2 for the results. Once the search is completed, I want the user to be able to click a row in DataGridView1 (which contains the FullDocNo and DocType) and have the full text (from the database column “SectionText) show up in TextBox2.

The end result will be similar to the effect gained by simply dragging the “FullDocuments” (DataGridView) table and “SectionText” (TextBox) column onto the form and allowing the BindingNavigator to bind and coordinate the content of the two controls. Only I want to be able to do a word search instead of relying on the navigator. This is the approach used by popular search engines like Google where a summary of the search results are returned and the user can click an item title to see the full document. I can get the appropriate search results summary into DataGridView1, but no full text search results in TextBox2.

The obvious solution seemed to be using the Property Window to set the DataBindings (Text) property of TextBox2 to the “SectionText” column of the FullDocumentsBindingSource. I understand that the BindingSource is responsible for coordinating the content of the various controls. It just doesn’t seem to work. So I am trying to approach the problem programmatically with code. Here is what I have so far:

'Fills the DataGrid View with search results summary

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Dim ds As New DocumentsDataSet

Dim ta As New DocumentsDataSetTableAdapters.FullDocumentsTableAdapter

ta.FillBy(ds.FullDocuments, String.Format("{0}{1}{0}", "%", TextBox1.Text))

DataGridView1.DataSource = ds.FullDocuments

End Sub

'Fills the search results text box with text from "SectionText" column of database

Private Sub DataGridView1_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick

If Me.DataGridView1.SelectedCells.Count = 1 Then

TextBox2.Text = Me.DataGridView1.SelectedCells(0).Value

End If

End Sub

This solution at least allows TextBox2 to link up with DataGridView1 and return whatever value is in the DataGridView1 column that is clicked. By including the SectionText column in the DataGridView1 and setting its column width properties to very small (since the SectionText can contain several pages of text), this does work in a crude way by clicking that column. But clicking any other column in DataGridView1 also loads that value into TextBox2.

There must be a simple way to get only the SectionText data into TextBox2 by clicking anywhere in a row in DataGridView1 (or a "Select" link column). Something similar to the GrideView and DetailsView relationship available in Visual Web Developer.

Any help in making this work would be greatly appreciated.



Answer this question

Filling TextBox with search results linked to DataGridView

  • A.M.

    Here is the code that solved my problem:

    Private Sub Button1_Click( _
    ByVal sender As System.Object, ByVal e As System.EventArgs) _
    Handles Button1.Click
    Dim ds As New DocumentsDataSet
    Dim ta As New DocumentsDataSetTableAdapters.FullDocumentsTableAdapter
    ta.FillBy(ds.FullDocuments, String.Format("{0}{1}{0}", "%", TextBox1.Text))

    Me.FullDocumentsBindingSource.DataSource = ds
    ' "FullDocuments" = name of FullDocuments.
    Me.FullDocumentsBindingSource.DataMember = "FullDocuments"
    Me.DataGridView1.DataSource = Me.FullDocumentsBindingSource
    Me.TextBox2.DataBindings.Clear()
    ' "SectionText" = name of column to bind TextBox2 to.
    Me.TextBox2.DataBindings.Add(New _
    System.Windows.Forms.Binding("Text", _
    Me.FullDocumentsBindingSource, "SectionText", True))

    End Sub


  • Filling TextBox with search results linked to DataGridView