2

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();     
}
3
  • use distinct keywords in the querry Commented Sep 24, 2016 at 12:23
  • Could you show the code in Add, Edit or Delete? I am unable to reproduce this behavior with the code provided. Commented Sep 24, 2016 at 12:58
  • I tried putting in the button for me to save / add items on the access. Commented Sep 24, 2016 at 13:12

1 Answer 1

0

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.

Sign up to request clarification or add additional context in comments.

5 Comments

Thank you very much. That works perfectly, i can finish my Project now.
Last question, On datagridview is there something like this - listView1.FullRowSelect = true; So that once they select a content, not only a shell but the full row will be selected, and editing the cells showing on datagrid will be disabled? Thanks!
Setting FullRowSelect doesn't disable editing. It just enable full row selection. If you have any problem with editing the rows, you may have some other issue, for example probably you set ReadOnly to true. If my comment was not helpful, then it's better to ask a new question about the new issue because question should be specific to a single problem.
Can you help me sir on this part. stackoverflow.com/questions/39688685/…
I read the question. Fortunately there was a correct answer for the question, I also added a comment to the answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.