简体   繁体   中英

How to display with case sensitive in datatable

I am putting some values in datatable from an xml and I have values that repeat and also some values case sensitive

ex: Cat,Elephant,Took,Program,Some,PROGRAM,Cat,Dog

so I am using one condition like

if (!(FindRow.Rows.Contains(val)))
{
   FindRow.Rows.Add(val);
}

I want my datatable to have both Program and PROGRAM , and should not have two cat values but its taking only one program

I can see at least two approaches:

  1. Prepare HashSet<string> and every time you add item into DataTable add lowercased version of the same string into the set. Then every time you want to add new row check the set insteaf of the table.

     var set = new HashSet<string>(); if (!(set.Contains(val.ToLower()))) { FindRow.Rows.Add(val); set.Add(val.ToLower()); } 
  2. You can use LINQ to perform a little more complicated query:

     if (!(FindRow.Rows.AsEnumerable().Any(r => r.Field<string>("YourKeyColumnName").ToLower() == val.ToLower()))) { FindRow.Rows.Add(val); } 

    You'll need System.Data.DataSetExtensions.dll assemble referenced and using System.Data.DataSetExtensions at top of the file to make it work.

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