Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Can't clear my DataGridView after updating a record

Posted on 2010-08-26
2
260 Views
Last Modified: 2012-08-14
Hello All:
I have two forms: One form contains a data grid with prospects.  When the user clicks a row a complete form opens, so the user can update the record.  After the record is updated (which works) the record now is in the database, but the data grid in the first form is not updating.  I placed a refresh button in form one to test the code before automating the refresh.  Please look at my code.  I tried lots of thigs and what I AM GETTING IS THAT WHEN I REFRESH THE DATA GRIDVIEW SHOWS THE DATA TWICE.  THE OLD DATA AND THE NEW DATA.  Setting the datagridview to nothing does not clear the old data.  also, I tried refresh after nothing and I also tried Rows.Clear.  I get the same result. My datagridview's data source is a datatable. Please help.  
Gloria
Shared WithEvents myConnection As SqlConnection
    Shared clsProspect As New clsProspect
    Shared dt As New DataTable
    Shared dtNotInterested As New DataTable
    Shared da As SqlDataAdapter
    Shared sqlCommand As SqlCommand
    Shared sqlConnection As New SqlConnection(strConn)

    Private Sub frmShowAllProspects_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Call fillGrids()
    End Sub
    Private Sub fillGrids()
       
        sqlCommand = New SqlCommand("getprospecthistory", sqlConnection)
        
        sqlCommand.CommandType = CommandType.StoredProcedure
        
        da = New SqlDataAdapter(sqlCommand)

        Try

            da.Fill(dt)
            
        Catch ex As Exception
            MsgBox(ex.Message & " " & ex.Source)
            If sqlConnection.State <> ConnectionState.Closed Then
                sqlConnection.Close()

            End If
            sqlCommand = Nothing
            sqlCmdNotInterested = Nothing
            da = Nothing
            daNotInterested = Nothing

        End Try
        mydgCustomer.DataSource = dt
        Me.mydgCustomer.Columns(9).Width = 0
    End Sub
    

    Private Sub btnRefresh_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRefresh.Click

        Me.mydgCustomer.DataSource = Nothing
        da = New SqlDataAdapter(sqlCommand)
        da.Fill(dt)
        Me.mydgCustomer.DataSource = dt
    End Sub

    Protected Overrides Sub Finalize()
        MyBase.Finalize()
        sqlCommand = Nothing
        sqlConnection = Nothing
        da = Nothing
        dt = Nothing

    End Sub

Open in new window

0
Comment
Question by:gloriagalvez
2 Comments
 
LVL 3

Accepted Solution

by:
PHaddock earned 500 total points
ID: 33533479
Looks like you're not refreshing the datatable
0
 

Author Closing Comment

by:gloriagalvez
ID: 33533830
Yes, that was silly, you definitely need to refresh the data table.  I thought that setting the data source to nothing would do.  Thank you.. Please see final code for refresh button below.
Private Sub btnRefresh_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRefresh.Click

        Me.mydgCustomer.DataSource = Nothing

        da = New SqlDataAdapter(sqlCommand)
        dt.Clear()
        da.Fill(dt)
        Me.mydgCustomer.DataSource = dt


    End Sub
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question