disappearing datagrid

W

wk6pack

Hi,

I have a datagrid that is populated from a query in SQL Server. The first
time I run the form, it appears, I make changes or view it and it is fine.
When I close the form and open it up again, the datagrid is not there to
see. I'm running the same code to populate it. What is happening? Any
suggestions?

thanks,
Will

Here is the code.



Dim ProcName As String = "PopulateDataGrid"

Dim daRoleDetail As SqlDataAdapter

Dim conn As SqlConnection

dsClass.Clear()

Me.dgClass.TableStyles.Clear()

Try

conn = New SqlConnection(UserInfoClass.sconn)

daRoleDetail = New SqlDataAdapter(SqlQuery, conn)

daRoleDetail.Fill(dsClass, TableName)



Dim ts As New DataGridTableStyle

ts.MappingName = TableName

ts.AlternatingBackColor = Color.WhiteSmoke

ts.SelectionBackColor = Color.Yellow

ts.SelectionForeColor = Color.Red

ts.PreferredRowHeight = 22

Dim colID As New DataGridTextBoxColumn

Dim colRoleDetail As New DataGridTextBoxColumn

Dim colclass As New DataGridTextBoxColumn

Call setDataGridProps(colID, "RoleDetailID", "ID", "", 75)

Call setDataGridProps(colRoleDetail, "RoleDetail", "Role Detail", "", 100)

Call setDataGridProps(colclass, "class", "Class", "", 200)

ts.GridColumnStyles.Add(colRoleDetail)

ts.GridColumnStyles.Add(colclass)

Me.dgClass.TableStyles.Add(ts)

ts = Nothing

Me.dgClass.SetDataBinding(dsClass, TableName)

Me.displayMsg(CStr(dsClass.Tables(TableName).Rows.Count) & " " & TableName &
" Records Loaded")

'clean up resources

If Not conn Is Nothing Then

conn.Close()

conn.Dispose()

End If

If Not daRoleDetail Is Nothing Then daRoleDetail.Dispose()
 
C

Cor Ligthert [MVP]

WK6pack,

A datagrid does not hold data, it is displaying it only from its datasource.
(the binding to that)

If the data is not anymore there (as it seems to me in your program), the
datagrid will as well not dispay that.

Cor
 
W

wk6pack

Hi Cor,

I am getting the data from the query every time I display the grid. It
works on the first run of the form. If I close the form and open the form
by clicking on the button again, the datagrid doesnt get displayed but it
shows a record count in a messagebox.
If I get data from the query the first time and it displays why wont it
display the second time running the same procedure call?

Where in my program do you think the data is not there anymore? I'm using a
"Select * from table" as SqlQuery.

---Getting the data from the database----
conn = New SqlConnection(UserInfoClass.sconn)
daRoleDetail = New SqlDataAdapter(SqlQuery, conn)
daRoleDetail.Fill(dsClass, TableName

---setting the datagrid datasource with the dataset------
Me.dgClass.SetDataBinding(dsClass, TableName)

thanks,
Will
 
C

Cor Ligthert [MVP]

Can you show us what you mean, (some code) with closing a form. Standard is
that closing the whole program. (The term open in VB.Net as far as I know
not used.

(Or is should be a file or something like that). What you can do is create a
new form.

Cor
 
W

wk6pack

Hi Cor,

Private Sub FrmClass_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles MyBase.Load
call centerForm()
call PopulateDataGrid()
end sub

Here is the close form code.
ButtonClose:
dsTrainType.Dispose()

Me.dgTrainingType.Dispose()

Close()

thanks,
Will
 
C

CodeMonkey

Are you creating a new form (FrmClass) every time you open the form or
are you creating it once and then just .Show() it when you want to
display it?
 
C

CodeMonkey

Then my guess is that you are disposing of the controls on the form and
then trying to show that same form again. When you dispose the controls,
they are gone. You will have to create a new instance of the form before
you can Show it again.
 
W

wk6pack

Thanks CodeMonkey. That was the problem. I was disposing the datagrid when
the form closed. Once I removed it, it works like a charm.

thanks again.
Will
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top