none
How can i delete duplicate row from select after insert statement? RRS feed

All replies

  • I'm not exactly sure what you are doing.

    How are you identifying the row in the datagrid to actually copy for your new insert? Because you can just remember either the row index or a suitable ID that you know is unique from that row at the time you determine the row to copy.

    Then use that information to delete the row afterwards.

    (NB: The row index will range from 0 to the Count -1. That's why you get an index out of range error trying to delete the rowindex of dg.Rows.Count).

    Tuesday, June 26, 2018 7:44 AM
  • Thank you for your response.I understand what you're saying and i have my project connected to the database and i'm using a dataset.I don't know if i should make a query or i should relate strictly to the datagridview but then how can i say that for eg. if column has the same serial nr(SN),then it should delete the row with that value? 

    Best regards,

    Tuesday, June 26, 2018 8:07 AM
  • You haven't said how you are populating your datagridview with your query.

    Assuming it is bound though to a DataTable then you probably want to delete it from the source and this change will be reflected in the datagrid view.

    string expression = "strNr = "+someId.ToString();
    DataRow[] foundRows;
    // Assuming table is the datasource to which your datagridview is bound...
    foundRows = table.Select(expression);
    
    if (foundRows.Count > 0)
    {
       foundRows.Delete();
    }
    
    
    Alternately, you can loop through the rows of your datagridview until you find the row you want to remove, similar to the answer here: https://stackoverflow.com/questions/10179223/find-a-row-in-datagridview-based-on-column-and-value

    Tuesday, June 26, 2018 8:22 AM
  • Thank you so much for your help.I will try it to see if it will work and get back to you later.

    Best regards,

    Maria

    Tuesday, June 26, 2018 8:51 AM
  • I doesn't work.I have initialized it in Form_load but it's like it's not there.I have also looked at what you propose me and I also tried one of the methods there but without any luck.I'll keep searching.Thank you anyways for your time and help.

    Best regards,

    Maria

    Tuesday, June 26, 2018 9:11 AM
  • It may help if you posted up a bit more of your code.

    Specifically, how you are selecting the data, how you are binding that data into your datagridview and how you are later determining which row is the basis for your new insert statement.

    Tuesday, June 26, 2018 9:24 AM
  • Hi,

    Thanks for posting here.

    From your description, you may perform select and insert statement on the same data source, which results in that after you perform insert statement the datagridview show the duplicate records. For your requirements, you can try update statement to replace insert statement. In this case, there is no need to perform delete execution.

    Besides, it would be helpful to provide a sample to show your question directly.

    Regards,

    Dolen


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, June 27, 2018 6:48 AM