6 Replies - 3090 Views - Last Post: 26 June 2012 - 02:45 PM Rate Topic: -----

#1 STP_Captain_Slow  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 75
  • Joined: 08-October 10

VB.Net - DataGridView - Won't populate second time form is called

Posted 28 January 2011 - 12:18 PM

Hi

I am building a program where for part of it the user can enter a parameter which will then query a database and populate a DataTable with the results if the number of results is greater than 1 it will call a "lookup" form to popuate a DataGridView with the DataTable, from this the user can select a row and the result is passed back to the main form or if the result is not there the "lookup" can be closed.

This works fine the first time the parameter is used to query and opens the "lookup" form and populates the DGV, but if the user returns the the main program after using the lookup form and enters a new parameter and the lookup form opens again the DataGridView is not created and populated. But i am 99% sure the DateTable is being populated as if the row count is 1 it will automatically return the single value and this is happening even after the lookup form has been used.

So it is just when the Lookup form is being used a second time the DataGridView is not being popuated or the DataTable is not being passed.

Any help would be very grateful as i have being trying differnt things all day. Please find below snippets of my code which basically shows the opening of the Lookup form and how i am passing the DataTable, and how the Lookup Form uses the DataTable. I have also attached 2 screenshots showing the Lookup form the first time its used and then when it is recalled.


'This performs the query on the database and populates the DataTable if the results (if any)
Call DescSearch()

'Checks if the Search has produced more than 1 row of result
If DTDesc.Rows.Count > 1 Then

 'If so open the Lookup form to show results
 Dim frmDescSearch As New frmDescSearch

 'Pass the DataTable to the Lookup Form and show the form
 frmDescSearch.DTDesc = Me.DTDesc
 frmDescSearch.ShowDialog()

ElseIf DTDesc.Rows.Count = 1 Then
 'If the Search has produced only one result apply this to the text box
 txtDesc.Text = LTrim(DTDesc.Rows(0)("Description").ToString)

End If
'Clear the DataTable ready for next search if required
DTDesc.Clear()



This is the code for lookup form, when it opens is creates the DataSet attaches the DataTable to the DataGridView and sets a couple parameters within the DGV.


Public Class frmDescSearch

    Public DTDesc As DataTable
    Public DSDesc As DataSet


    Private Sub frmDescSearch_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Call InitializeTable()

        If dgvDesc.Rows.Count > 0 Then

            dgvDesc.Rows(0).Selected = False

        End If

    End Sub

    Private Sub InitializeTable()

        DSDesc = New DataSet()
        DSDesc.Tables.Add(DTDesc)
        dgvDesc.DataSource = DSDesc
        dgvDesc.DataMember = "Search"

        dgvDesc.Columns("Description").ReadOnly = True

        Dim xCol As New DataGridViewColumn
        'Dim xRow As New DataGridViewRow

        xCol = dgvDesc.Columns(0)
        xCol.Width = 200

        dgvDesc.Font = New Font("Arial", 9)

    End Sub
End Class





When i get the Lookup form working correctly, this form will either return the selected result or the form will be closed back to the Main form.

Again thanks in advance for any help. I will be more than happy to answer any questions regarding other code i haven't included which may be causing this but at the minute i don't understand why this is happening.

Attached image(s)

  • Attached Image
  • Attached Image


Is This A Good Question/Topic? 0
  • +

Replies To: VB.Net - DataGridView - Won't populate second time form is called

#2 _HAWK_  Icon User is offline

  • Master(Of Foo)
  • member icon

Reputation: 1057
  • View blog
  • Posts: 4,099
  • Joined: 02-July 08

Re: VB.Net - DataGridView - Won't populate second time form is called

Posted 28 January 2011 - 06:33 PM

You set the DGV's.DataSource to the dataset where you need to set it to a table on the dataset.
Was This Post Helpful? 1
  • +
  • -

#3 STP_Captain_Slow  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 75
  • Joined: 08-October 10

Re: VB.Net - DataGridView - Won't populate second time form is called

Posted 28 January 2011 - 06:45 PM

hawkvalley1: Thanks for that, that seems to of solved that problem but do you know why my DGV Parameters are now not working as the coloumn is now not read only and the width of the column is wrong?

the frmDescSearch code is the same apart from i have removed the DS statements and now the DataSource = DTDesc

Thanks again

EDIT:

I have commented out the below line and the parameters are working again is the line needed or can i do with out? I am still quite new to the different ways of attching data to DGV's and picked the above off a different example on the web.

dgvDesc.DataMember = "Search"

This post has been edited by STP_Captain_Slow: 28 January 2011 - 06:51 PM

Was This Post Helpful? 0
  • +
  • -

#4 _HAWK_  Icon User is offline

  • Master(Of Foo)
  • member icon

Reputation: 1057
  • View blog
  • Posts: 4,099
  • Joined: 02-July 08

Re: VB.Net - DataGridView - Won't populate second time form is called

Posted 28 January 2011 - 07:01 PM

No it is not required.
Was This Post Helpful? 0
  • +
  • -

#5 _HAWK_  Icon User is offline

  • Master(Of Foo)
  • member icon

Reputation: 1057
  • View blog
  • Posts: 4,099
  • Joined: 02-July 08

Re: VB.Net - DataGridView - Won't populate second time form is called

Posted 28 January 2011 - 07:14 PM

Also you don't have to use a dataset, a datatable will do the same thing all by it's self.
Was This Post Helpful? 0
  • +
  • -

#6 STP_Captain_Slow  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 75
  • Joined: 08-October 10

Re: VB.Net - DataGridView - Won't populate second time form is called

Posted 29 January 2011 - 01:50 AM

Excellent, thanks for your help, now to move on!
Was This Post Helpful? 0
  • +
  • -

#7 apprabhu  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 08-April 11

Re: VB.Net - DataGridView - Won't populate second time form is called

Posted 26 June 2012 - 02:45 PM

Hi

I'm also facing a problem similar to this. I don't know what I'm missing. I have used combobox selectedindexchanged event to populate the dgv. It shows the data in the datatable for the first time of the form loaded. When i change the combobox value it shows a empty dgv. It works when i close and reopens the form.

Here is my code...

Private Sub cmbFromSec_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbFromSec.SelectedIndexChanged

        Dim boolCol As New System.Windows.Forms.DataGridViewCheckBoxColumn
        dgvStudFrom.DataSource = Nothing
        dgvStudFrom.Rows.Clear()
        dgvStudFrom.Columns.Clear()

        dtSP.Clear()
        dtSP = objSP.retDT("SELECT tblStudent.stu_id,tblStudent.stu_admissionnum AS [Admn No], tblStudent.stu_name AS [Stu Name]FROM tblClass INNER JOIN tblStudent ON tblClass.cls_id = tblStudent.stu_classid WHERE(((tblClass.cls_name) = '" & cmbFromClass.Text & "') And ((tblClass.cls_section) = '" & cmbFromSec.Text & "') And ((tblStudent.stu_status) = True) And ((tblStudent.stu_studying) = True))")
        dgvStudFrom.DataSource = dtSP

        dgvStudFrom.Columns(0).Visible = False
        dgvStudFrom.Columns(1).ReadOnly = True
        dgvStudFrom.Columns(2).ReadOnly = True
        dgvStudFrom.Columns.Add(boolCol)

        dgvStudFrom.Columns(1).Width = 100
        dgvStudFrom.Columns(2).Width = 150
        dgvStudFrom.Columns(3).Width = 80

    End Sub


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1