Results 1 to 10 of 10

Thread: Reuse of DGV doesn't fill the grid

  1. #1

    Thread Starter
    New Member
    Join Date
    Nov 2017
    Posts
    9

    Question Reuse of DGV doesn't fill the grid

    I have an invoice form using a data grid (DGVInvoice). When I start to enter a description, this shows another grid in the same Form (DGVMatches) that shows all matches from the Invoice/Description field in the database. Clicking any of the matches updates the DGVInvoice description field.

    This works perfectly on Line 1 of the invoice, but when I enter a description on subsequent lines, no matches are displayed in DGVMatches (although the DGVMatches header shows the search term correctly).

    Code in DGVInvoice to call DGVMatches:

    Code:
    Private Sub ShowMatches()
    
          ' @@ Fetch Descriptions from LineEntry table
          Dim pSQL As String = "SELECT DISTINCT [Desc] FROM LineEntry WHERE [Desc] LIKE '" & mCellContent & "%' ORDER BY [Desc] "
          Dim m_Datatable As New DataTable
    
          Using cnn As New OleDbConnection(gs_Connection)
              cnn.Open()
              Using dad As New OleDbDataAdapter(pSQL, cnn)
                  dad.Fill(m_Datatable)
                  dad.Dispose()
              End Using
              cnn.Close()
          End Using
    
          GbMatching.Text = "Matching entries for '" & mCellContent & "'"
    
          If m_Datatable.Rows.Count = 0 Then  ' ** No data
              GbMatching.Visible = False
          Else
              With DgvMatches
                  .DataSource = m_Datatable
                  GbMatching.Visible = True
                  .Columns(0).Width = 360
                  .Columns(0).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleLeft   ' @@ Aligns cells
                  .Refresh()
              End With
              m_Datatable = Nothing
          End If
    
        End Sub
    Code in DGVMatches to fill the DGVInvoice Description field:

    Code:
    Private Sub DgvMatches_Click(sender As Object, e As EventArgs) Handles DgvMatches.Click
          With DgvInvoice
              .CurrentCell.Value = DgvMatches.CurrentCell.Value         
              DgvInvoice.BeginEdit(False)
          End With
    
          GbMatching.Visible = False
          mMatchFound = True
          DgvMatches.Dispose()
    
      End Sub
    Can anyone help with this?
    Last edited by jmcilhinney; Jul 9th, 2024 at 12:04 AM.

  2. #2
    Fanatic Member
    Join Date
    Jul 2022
    Location
    Buford, Ga USA
    Posts
    630

    Re: Reuse of DGV doesn't fill the grid

    Have you stepped through the code with debugging? Maybe run the query manually to ensure it should be returning records?

  3. #3

    Thread Starter
    New Member
    Join Date
    Nov 2017
    Posts
    9

    Re: Reuse of DGV doesn't fill the grid

    If I select from the DGVMatches grid, the following line fails to display the matching data but as I said, the Matches header does show the search term.
    If I don't select from the Matches grid, the following line does show all matches.
    I think that the problem is in the ShowMatches routine, because of the correct header, and when debugging, the Row.Count is more than zero. Nothing is showing any errors.

    I'm stuck because as the code operates correctly on the first row that it's used, the code would seem to be correct. The only thing I can think of is that I haven't closed the connection or DGVMatches correctly so conditions are somehow different on the second iteration, but it all looks ok to me.

    Puzzled of Cambridge!

  4. #4
    PowerPoster
    Join Date
    Sep 2005
    Location
    Modesto, Ca.
    Posts
    5,508

    Re: Reuse of DGV doesn't fill the grid

    I don't see where "ShowMatches" is called from. You need to include that.

    Also, always wrap the code in code tags. Just click "#" and insert the code. That way it retains the formatting and easier to read.

  5. #5

    Thread Starter
    New Member
    Join Date
    Nov 2017
    Posts
    9

    Re: Reuse of DGV doesn't fill the grid

    Oops! Thanks for the tip about the # wrapper.

    I should have mentioned that DGVMatches is inside a group box called GbMatching.
    GbMatching.text inside ShowMatches always shows the search string, so the query must be working.

    Thanks for your help.
    Code:
       Private Sub DgvInvoice_EditingControlShowing(sender As Object, e As DataGridViewEditingControlShowingEventArgs) Handles DgvInvoice.EditingControlShowing
           EditingTextBox = TryCast(e.Control, TextBox)
       End Sub
       
       Private Sub Editingtextbox_KeyDown(sender As Object, e As KeyEventArgs) Handles EditingTextBox.KeyDown
           ' @@ Handles call to ShowMatches 
           If EditingTextBox IsNot Nothing Then
               If DgvInvoice.CurrentCell.ColumnIndex = 4 Then  ' @@ Description cell for invoice line
                   mCellContent += (e.KeyCode).ToString
     
                   ' @@ Show Autofill matches
                   If Len(mCellContent) > 0 Then
                       GbMatching.Visible = True
                       ShowMatches()
                   Else
                       GbMatching.Visible = False
                   End If
               End If
           End If
       End Sub
    Last edited by jmcilhinney; Jul 9th, 2024 at 12:03 AM.

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Reuse of DGV doesn't fill the grid

    Quote Originally Posted by RafStorr View Post
    Oops! Thanks for the tip about the # wrapper.
    Except you got it wrong. They didn't say to type that character. They said to click it, i.e. click the toolbar button with that symbol on it. Surely you've used text editors with toolbars for formatting before. I fixed it for you this time.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7

    Thread Starter
    New Member
    Join Date
    Nov 2017
    Posts
    9

    Re: Reuse of DGV doesn't fill the grid

    Thank you - no I haven't seen this before, but then I've never posted for help before either.

  8. #8
    PowerPoster
    Join Date
    Sep 2005
    Location
    Modesto, Ca.
    Posts
    5,508

    Re: Reuse of DGV doesn't fill the grid

    You use mCellcontent as your search parameter, I don't know what's in the Desc field but this seems strange,

    Code:
    mCellContent += (e.KeyCode).ToString
    You shouldn't concatenate strings like that, it should be mCellContent &= (e.KeyCode).ToString.

    Unless mCellContent isn't a string but if that's true then this is wrong, " [Desc] LIKE '" & mCellContent & "%' "

    mCellContent will just get larger and larger. Maybe that's correct, I don't know. I'd suggest putting a breakpoint here "If Len(mCellContent) > 0 Then" and check the value of mCellContent.

  9. #9

    Thread Starter
    New Member
    Join Date
    Nov 2017
    Posts
    9

    Re: Reuse of DGV doesn't fill the grid

    Problem resolved. I had a line in other code that stopped this code from working properly.
    All the code listed in this thread works correctly.

    Sorry if I caused some head scratching...

  10. #10

    Thread Starter
    New Member
    Join Date
    Nov 2017
    Posts
    9

    Re: Reuse of DGV doesn't fill the grid

    Thanks to wes4dbt - I have corrected my concatenation. (Yes, mCellContent is a string).

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width