Refresh datagridview after db update

Posted on 2008-11-10
Last Modified: 2012-05-05
Alrighty I update, and delete in my acess db.  However after doing that I hit my button to search again and its adding the results to the bottom of my previous results instead of getting rid of old ones and then putting in new ones.  any ideas?
                OdbcConnection con = new OdbcConnection("DSN=ShipData");

                OdbcDataAdapter da = new OdbcDataAdapter();

                DataTable dt = new DataTable();

                string sqlQuery = "SELECT * FROM Address Order by Code";

                OdbcCommand com = new OdbcCommand(sqlQuery, con);

                da.SelectCommand = com;

                OdbcCommandBuilder cmbuilder = new OdbcCommandBuilder(da);

                cmbuilder.QuotePrefix = "[";

                cmbuilder.QuoteSuffix = "]";


                dataGridView1.DataSource = dt;

Open in new window

Question by:casit
    LVL 39

    Expert Comment

    by:Pratima Pharande
    You mean to say you want direct reresh after update and delete without using busston clik ?

    Then do one this put above acode in one function..

    and call that function after update and delete
    LVL 7

    Expert Comment

    Clear the table first on button click:


    before adding doing the SELECT command

    Author Comment

    When I put rows.clear in I get the following exception report
    System.ArgumentException was unhandled
      Message="Cannot clear this list."
           at System.Data.DataView.System.Collections.IList.Clear()
           at System.Windows.Forms.DataGridViewRowCollection.Clear()
           at FedEx_Shared_Addy.frmSharedAddy.loadAllAddresssToolStripMenuItem_Click(Object sender, EventArgs e) in C:\Users\chris\Documents\Visual Studio 2005\Projects\FedEx Shared Addy\Form1.cs:line 62
           at FedEx_Shared_Addy.frmSharedAddy.Loadalldata_Click(Object sender, EventArgs e) in C:\Users\chris\Documents\Visual Studio 2005\Projects\FedEx Shared Addy\Form1.cs:line 83
           at System.Windows.Forms.Control.OnClick(EventArgs e)
           at System.Windows.Forms.Button.OnClick(EventArgs e)
           at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
           at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
           at System.Windows.Forms.Control.WndProc(Message& m)
           at System.Windows.Forms.ButtonBase.WndProc(Message& m)
           at System.Windows.Forms.Button.WndProc(Message& m)
           at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
           at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
           at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
           at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
           at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
           at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
           at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
           at System.Windows.Forms.Application.Run(Form mainForm)
           at FedEx_Shared_Addy.Program.Main() in C:\Users\chris\Documents\Visual Studio 2005\Projects\FedEx Shared Addy\Program.cs:line 17
           at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
           at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
           at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
           at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
           at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
           at System.Threading.ThreadHelper.ThreadStart()

    Open in new window


    Accepted Solution

    nm I figured it out.  I had to first clear my datatable and then everything was good.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Article by: Ivo
    Anonymous Types in C# by Ivo Stoykov Anonymous Types are useful when  we do not need to follow usual work-flow -- creating object of some type, assign some read-only values and then doing something with them. Instead we can encapsulate this read…
    Article by: Najam
    Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
    Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
    Here's a very brief overview of the methods PRTG Network Monitor ( offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

    779 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

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now