?
Solved

Refresh datagridview after db update

Posted on 2008-11-10
4
Medium Priority
?
1,487 Views
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 = "]";
                da.Fill(dt);
                dataGridView1.DataSource = dt;

Open in new window

0
Comment
Question by:casit
  • 2
4 Comments
 
LVL 39

Expert Comment

by:Pratima Pharande
ID: 22919933
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
0
 
LVL 7

Expert Comment

by:aherps
ID: 22919950
Clear the table first on button click:

dataGridView1.Rows.Clear();

before adding doing the SELECT command
0
 

Author Comment

by:casit
ID: 22922256
When I put rows.clear in I get the following exception report
System.ArgumentException was unhandled
  Message="Cannot clear this list."
  Source="System.Data"
  StackTrace:
       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

0
 

Accepted Solution

by:
casit earned 0 total points
ID: 22922571
nm I figured it out.  I had to first clear my datatable and then everything was good.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
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…
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
Suggested Courses

809 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