DataGrid In Code

SirMiltS

New member
Joined
Oct 24, 2009
Messages
3
Location
florida
Programming Experience
5-10
I am using VB.Net 2003 with an Access 2000 database. The program contains three options for the content of a DataGrid, each from a different table. I have finished programming the DataGrid which loads as the default when the program starts.

When I run the program the DataDrid appears, but it is empty, except for a small box with a "+" in it. I click the + and the table name appears, I click the table name and the DataGrid is populated exactly as I wanted it.

My problem is that I don't want a User to have to do that, I want the DataGrid populated when the program loads. I have tried to find some reference to this problem, but can't.


Does anyone have any suggestons?
 
How exactly are you populating the grid? My guess is that you're assigning the DataSet to the DataSource but you're not assigning the name of the DataTable to the DataMember.
 
Thank you for the quick response. Here's my code:
VB.NET:
    Private Sub FillBookGrid()
        strSelectedCollection = "Book"
        strSQL = "lib_BookGridSelect"
        'Select the query to provide data...
        Try
            da.SelectCommand = New OleDbCommand
            da.SelectCommand.Connection = cn
            da.SelectCommand.CommandText = strSQL
            da.SelectCommand.CommandType = CommandType.StoredProcedure
            cn.Open()
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
        'Open the database connection...
        da.SelectCommand.ExecuteNonQuery()
        'Set tha data sources fo the grids...
        da.Fill(ds, "tblBooks")
        'Close the connection... 
        cn.Close()

        'Set the dataGrid properties to bind it to the data...
        grdCollection.DataSource = ds
        grdCollection.DataMember = "tblBooks"
        'Declare objects for the data grid
        Dim objGridTablesStyle As New DataGridTableStyle
        Dim objTextCol As New DataGridTextBoxColumn

        'Set the alternating color of the Books DataGrid...
        objGridTablesStyle.AlternatingBackColor = Color.WhiteSmoke
        'Set the DataGrid mapping for Books...
        objGridTablesStyle.MappingName = "tblBooks"

        'Set the MappingName for the ID column...
        objTextCol.MappingName = "ID"
        'Set the new HeaderText...
        objTextCol.HeaderText = "ID"
        objTextCol.Width = 50
        'Add the column to the DataGridTableStyle...
        objGridTablesStyle.GridColumnStyles.Add(objTextCol)

        'Set the MappingName for the Title column...
        objTextCol = New DataGridTextBoxColumn
        objTextCol.MappingName = "Title"
        'Set the new HeaderText...
        objTextCol.HeaderText = "Title"
        objTextCol.Width = 250

        'Add the column to the DataGridTableStyle...
        objGridTablesStyle.GridColumnStyles.Add(objTextCol)

        'Set the MappingName for the Authors column...
        objTextCol = New DataGridTextBoxColumn
        objTextCol.MappingName = "Authors"
        'Set the new HeaderText...
        objTextCol.HeaderText = "Author(s)"
        objTextCol.Width = 250
        'Add the column to the DataGridTableStyle...
        objGridTablesStyle.GridColumnStyles.Add(objTextCol)

        'Set the MappingName for the Series column...
        objTextCol = New DataGridTextBoxColumn
        objTextCol.MappingName = "Series"
        'Set the new HeaderText...
        objTextCol.HeaderText = "Series"
        objTextCol.Width = 250
        'Add the column to the DataGridTableStyle...
        objGridTablesStyle.GridColumnStyles.Add(objTextCol)

        'Add the DataGrid TableStyle to the DataGrid...
        grdCollection.TableStyles.Add(objGridTablesStyle)

    End Sub
What really confuses me is that if I move the code to the FormLoad event it works fine. I tried to test the sequence of the events executing, and those that I wrote ("FillBookGrid") above execute before the FormLoad event, I was under the impression that the FormLoad executes when the form loads and any procedures that I write execute only when called. Is VB>Net 2003 different?

Milt
 
Last edited by a moderator:
Problem solved.

The problem was that I had a radio buttn with the "Checked" property set to "True". The form loaded and went to the button checked procedure and then directly to the code without completing the Form Load.

I changed the Radio Button Checked to "False", added a line of code to the Form Load to check the button and it works exactly as I wanted it to.

Milt
 
Your following line(s) of code are not needed:

VB.NET:
'not needed: da will open the connection itself   
        'Open the database connection...
        da.SelectCommand.ExecuteNonQuery()

'not needed: da will close the connection itself
        'Close the connection... 
        cn.Close()
 

Latest posts

Back
Top