13342

How to Reload DataGridView using DataTable.Load(DataReader) without duplicate records

Can anyone help me on how to avoid getting duplicated output on DataGridView. Here is the image Before change and the image After add/edit or delete.

Here is the loader for my DataGridView:

private DataTable data() { using (OleDbConnection con = new OleDbConnection(inventorydb)) { using (OleDbCommand com = new OleDbCommand("Select * FROM Items",con)) { con.Open(); OleDbDataReader reader = com.ExecuteReader(); items.Load(reader); } } return items; } void reset() { connect.Close(); connect.ConnectionString = inventorydb; connect.Open(); dataGridView1.DataSource = null; dataGridView1.Update(); dataGridView1.Refresh(); dataGridView1.DataSource = data(); }

Add and save changes:

private void save_Click(object sender, EventArgs e) { if (mode == "a") { connect.Close(); connect.ConnectionString = inventorydb; connect.Open(); sqlcommand.CommandText = "INSERT INTO Items (SerialID, Brand_Name, Item_Name,Item_Date) VALUES ('" + txtserial.Text + "','" + txtbrand.Text + "', '" + txtitem.Text + "', '" + date + "') "; sqlcommand.Connection = connect; OleDbDataReader reader = sqlcommand.ExecuteReader(); MessageBox.Show("Record(s) Saved", "Sample"); } connect.Close(); reset(); }

Answer1:

DataTable.Load adds result to the existing rows by default. So if you need to just have latest records, before calling DataTable.Load, you should clear the DataTable, otherwise the new result will be merged with existing rows.

So with your items variable which is DataTable, you can use either of these options:

items= new DataTable();

or

items.Clear(); items.AcceptChanges();

DataTable.Load

Fills a DataTable with values from a data source using the supplied IDataReader. If the DataTable already contains rows, the incoming data from the data source is merged with the existing rows.

Recommend

  • Getting Transact SQL query result in C# Application
  • My database system cannot find the file specified in asp.net
  • Invalid attempt to read when no data is present in ASP.NET
  • uncommittable transaction is detected at the end of batch. the transaction is rolled back
  • Updating an Access Database via a DataGridView Using OLEDB in VB.NET
  • Azure service bus paired namespace - simulate failover
  • What is using connection in the code
  • How to extract a number from a string [duplicate]
  • python - calculate orthographic similarity between words of a list
  • Iterate twice through a DataReader
  • Python to parent/child JSON
  • Javascript focus remove text highlight
  • Multiple producers single consumer locking schema
  • Tell Git to stop prompting me for conflicts when none really exist?
  • MYSQ & MVC3 SQL connection error \\ ProviderManifestToken but I am using MySQL
  • Dynamically switching connect in Modelica
  • Ajax Upload File: $_FILES is empty but files exists in request header
  • C# program and C++ DLL compiled for 32-bit system crash on 64-bit system
  • ListItem.Attributes.Add not working
  • nonblocking BIO_do_connect blocked when there is no internet connected
  • Redux Form - Not able to type anything in input
  • Loading .coffee files via a view in Rails
  • Get history of file changes from TFS to implement custom “blame”-behaviour of exceptions
  • Create DicomImage from scratch using Dcmtk
  • Optimizing database types to compact database (SQLite)
  • TFS: Get latest causes slow project reloading
  • Running a C# exe file
  • php design question - will a Helper help here?
  • EntityFramework adding new object to nested object collection
  • Checking variable from a different class in C#
  • Django query for large number of relationships
  • Why is Django giving me: 'first_name' is an invalid keyword argument for this function?
  • Reading document lines to the user (python)
  • How can I use `wmic` in a Windows PE script?
  • failed to connect to specific WiFi in android programmatically
  • How to push additional view controllers onto NavigationController but keep the TabBar?
  • How can I use threading to 'tick' a timer to be accessed by other threads?
  • jQuery Masonry / Isotope and fluid images: Momentary overlap on window resize
  • How to load view controller without button in storyboard?
  • How do I use LINQ to get all the Items that have a particular SubItem?