简体   繁体   中英

C# WinForms Datagridview Check duplicates in a column

I have a following DatagridView, where a user can add, remove and update rows.

在此处输入图片说明

If a user adds a new row with an already submitted "KSTNR" value, a messagebox should appear.

My question is, how to loop through a column and check if a new "KSTNR" value has already been submitted.

Thank you!

I solved the problem using LINQ:

    //Get Values from Column "KSTNR"
    var kstnr = (from i in cpxx_helpDataSet.help_U_KSTKOSTEN
                orderby i.KSTNR
                select i.KSTNR).ToList();

    //Fill list with duplicates
     var query = kstnr.GroupBy(x => x).Where(g => g.Count() > 1).Select(y =>            y.Key).ToList();

    //If List not empty then show messagebox
    if(query.Any())
     {
          MessageBox.Show("Duplicates found");
     }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM