I have a program that will load a datagridview with data from a database via a dataset at form load. On this same form, I have a button named "count", that when pressed, I want it to show a message box indicating how many entries were loaded into the dataset.

My question is: How can I get the data from the main window into this click event sub??

or Do I have to fill the dataset again within the Click Event Sub??

The amount of entries is in variable named "numRowsFilled", which is set in this way...

Dim numRowsFilled As Interger = da.Fill(ds)

Any help would be appreciated...

CODE///

Public Class frmDBInterface

    Public Sub frmDBInterface_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        ' Setting up the connection string and the SQL command string
        Dim connString, sqlString As String
        connString = "Data Source=.\SQLExpress; Initial Catalog=testMovieDB; Integrated Security=True"
        sqlString = "SELECT MovieTitle, MovieGenre, MovieYear, MovieLocation FROM testMovies"
        'Initialize the data adapter and the data set
        Dim da As New SqlDataAdapter(sqlString, connString)
        Dim tableMap As DataTableMapping
        Dim columnMap As DataColumnMapping

        tableMap = da.TableMappings.Add("Table", "DVDMovies")
        columnMap = tableMap.ColumnMappings.Add("MovieTitle", "Title")
        columnMap = tableMap.ColumnMappings.Add("MovieGenre", "Genre")
        columnMap = tableMap.ColumnMappings.Add("MovieYear", "Year")
        columnMap = tableMap.ColumnMappings.Add("MovieLocation", "Location")
        da.MissingSchemaAction = MissingSchemaAction.AddWithKey

        Dim ds As New DataSet()
        Dim col As DataColumn

        With ds.Tables.Add("DVDMovies")
            col = .Columns.Add("Title", GetType(String))
            col.ReadOnly = True
            col.AllowDBNull = False
            col.MaxLength = 30
            col = .Columns.Add("Genre", GetType(String))
            col.AllowDBNull = False
            col.MaxLength = 20
            col = .Columns.Add("Year", GetType(String))
            col.AllowDBNull = True
            col.MaxLength = 4
            col = .Columns.Add("Location", GetType(String))
            col.MaxLength = 15
            col.AllowDBNull = True
            .PrimaryKey = New DataColumn() {.Columns("MovieTitle")}
        End With


        'Display the number of Movies Loaded into the DataGridView
        Dim numRowsFilled As Integer = da.Fill(ds)


        'Bind the datagrid to the datatable
        moviesDataGridView.DataSource = ds.Tables("DVDMovies")
        moviesDataGridView.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter

        'Set the colums to fill the width of the datagridview
        moviesDataGridView.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill

        moviesDataGridView.Columns(0).Width = 175
        moviesDataGridView.Columns(1).Width = 100
        moviesDataGridView.Columns(2).Width = 50
        moviesDataGridView.Columns(3).Width = 120

End Sub

 Private Sub btnCount_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCount.Click

MessageBox.Show("Total Number of Entries:   " &  numRowsFilled)
    End Sub
End Class

Move the declaration of the variable Dim numRowsFilled As Integer outside the Form_Load event and make it Private, thus making it a class variable.
Then it will be available from any event or method only within the class.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.