Duplicate rows in datagridview RRS feed

  • Question

  • Hello.

    I have a datagridview control that is being populated with some data from an XML stream. There is a timer that checks for new data and if found, is supposed to append rows to the control.

    The control allows a user to delete rows that have been reviewed.

    The first time the timer runs, the data are populated correctly, but each subsequent time, duplicate rows are created consisting of the old data from the prior run. The duplicate rows are the ones that have remained in the control so it appears they are being picked up again.

    The code is quite simple:

    Public Shared dt As New DataTable

    'this line of code is in a loop that appends data to the data source and is called by the timer

    'this section binds the data to the control
    Form1.DataGridView1.DataSource = dt

    I have tried using Refresh and other methods to no avail. I do not want to clear out the control or the contents of the data table. Only the user should be able to remove data so the use of something like:


    may not work because it will empty out the control as I understand it.

    The user should be able to delete any rows they don't want and the application should only add new rows to the control. So existing data should be kept in place and only new data should be added.

    Any help would be appreciated.

    Wednesday, March 18, 2009 10:33 PM


  • You might select the deleted rows and check whether the new rows can be found in the list of deleted rows.
    Maybe like this...

    // this is pseudo C#  
    // use a instance member instead of creating a new table each time new rows will be inserted  
    SomeDataSet dsData;  
    // ...  
    // your constructor code here...  
       // ...set up the DataSet and the grid...  
    OnNewDataAvailable(NewDataAvailableEventArgs args)  
      SomeDataSet.SomeDataDataTable deletedRecords = dsData.SomeData.GetChanges(DataRowState.Deleted) as SomeDataSet.SomeDataTable;  
      foreach (NewDataRowType values in args.Data)  
         // SomeDataRowTable should implement FindByID()  
         // The designer will add such a method for you, if you specify a primary key column  
         SomeDataSet.SomeDataRow deletedRow = deletedRecords.FindByID(values.ID);
         SomeDataSet.SomeDataRow existingRow = dsData.SomeData.FindByID(values.ID);
         // skip the row if it has been deleted or it's already listed
         if (deletedRow != null || existingRow != null) continue;  
         // ... add the row to the table  

    hope this helps,
    • Marked as answer by Bruce.Zhou Wednesday, March 25, 2009 9:55 AM
    Thursday, March 19, 2009 9:46 AM