second time it duplicates the datagridview

Opticknerve

New member
Joined
Jun 3, 2014
Messages
3
Programming Experience
Beginner
Hey i need help, when i click on my btnLoadeport button fror the first time it loads the datagridview perfectly, bt when i click on it for the second time it duplicates the datagridview.... how do i solve that?




VB.NET:
Public Class FrmMain
    
        Dim kena As New OleDb.OleDbConnection
        Dim dsSource As String
        Dim dsProvider As String
        Dim daAdapter As OleDb.OleDbDataAdapter
        Dim daset As New DataTable
        Dim sql As String
    
        Public Sub btnLoadReport_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLoadReport.Click
    
            dsProvider = "PROVIDER = Microsoft.Jet.OLEDB.4.0;"
            dsSource = "Data Source = C:\Users\Necromanga\Desktop\Databases\LecturerReport.mdb"
    
            kena.ConnectionString = dsProvider & dsSource
    
            kena.Open()
    
            Sql = "SELECT * FROM Report"
    
            daAdapter = New OleDb.OleDbDataAdapter(Sql, kena)
    
            daAdapter.Fill(daset)
    
    
            With Me.DataGridView1
                .Columns.Add("FacNum", "Faculty Number")
                .Columns.Add("LectNum", "Lecuturer Number")
                .Columns.Add("LecName", "Lecturer Name")
                .Columns.Add("ModNum", "Module Number")
                .Columns.Add("CredHours", "Credit Hours")
                .Columns.Add("ClassSize", "Class Size")
                .Columns.Add("ContactHours", "Contact Hours")
    
                .AllowUserToAddRows = False
                .EditMode = DataGridViewEditMode.EditProgrammatically
    
            End With
            For Each dr As DataRow In daset.Rows
                Me.DataGridView1.Rows.Add()
                With Me.DataGridView1.Rows(Me.DataGridView1.Rows.Count - 1)
                    .Cells("FacNum").Value = dr("FacNum")
                    .Cells("LectNum").Value = dr("LectNum")
                    .Cells("LecName").Value = dr("LecName")
                    .Cells("ModNum").Value = dr("ModNum")
                    .Cells("CredHours").Value = dr("CredHours")
                    .Cells("ClassSize").Value = dr("ClassSize")
                    .Cells("ContactHours").Value = dr("ContactHours")
                End With
    
            Next
    
            MsgBox("Connected to database")
    
            kena.Close()
            MsgBox("Connection terminated")
    
    
    
        End Sub
 
Hi,

Well it will do since you have declared your DataTable as a Member variable at the Class Level. Due to this, every time you click your button it will fill the DataTable with the results of the SQL Query duplicating those results each you press the button.

There are a few ways round this:-
1) Declare the DataTable locally within the Click Event of the button
2) Declare the DataTable as you have done at the Class Level and then populate your DataTable in the Form Load event of the Form.

Either way, you are making hard work of populating the DataGridView. All you need to do after your have filled the DataTable with your DataAdapter is to set the DataSource property of the DataGridView to the DataTable and all the work of populating the DataGridView is done for you.

If you also want to follow best practice then you should also add a BindingSource to your project, then set the DataSource property of the BindingSource to the DataTable and then set the DataSource property of the DataGridView to the BindingSource.

Hope that helps.

Cheers,

Ian
 
Back
Top