2016-10-31 3 views
0

다음 텍스트를 검색하여 DataGrid에 추가하는 기능이 있습니다.RegEx 일치에서 DataGrid에 중복 항목 피하기

try 
     { 
      string source = e.Result; 
      Regex re = new Regex(@"(\d+\.\d+\.\d+\.\d+):1400"); 
      MatchCollection mc = re.Matches(source); 

      if (mc.Count > 0) 
      { 
       foreach (Match matches in mc) 
       { 

        int index = dataGridAllSonos.Columns.ToList().FindIndex(c => c.Header == matches.Groups[1].Value); 
        Console.WriteLine(index); 


        var data = new sonosDevice 
        { 
         sonosIP = matches.Groups[1].Value, 
         sonosName = "XX", 
         sonosRoom = "XX" 
        }; 

        dataGridAllSonos.Items.Add(data); 


       } 

      } 

     } 
     catch (Exception ex) 
     { 
      if (ex.InnerException != null) 
      { 
       string err = ex.InnerException.Message; 
       Console.WriteLine(err); 
      } 
     } 

라인 int index = dataGridAllSonos.Columns.ToList().FindIndex(c => c.Header == matches.Groups[1].Value);는 전류가 IP (제 1 열)이있을 경우에 이미 존재하는 데이터 그리드에서 확인된다. 그런 다음 추가해서는 안됩니다.

불행히도 항상 -1을 반환하므로 IP가 중복되었는지 어떻게 확인할 수 있습니까?

sonosDevice 클래스

public class sonosDevice 
{ 
    public string sonosIP { get; set; } 
    public string sonosName { get; set; } 
    public string sonosRoom { get; set; } 
} 
+0

그 라인이 열 머리글에서 검색합니다. 이게 니가 원하는거야? 대신 행 내용을 검색해야합니다 –

+0

사실 저는 열 값을 실제로 찾습니다 ... – PrimuS

답변

0

이 시도 :

bool found = dataGridAllSonos.Items.OfType<string>().Any(i => i == matches.Groups[1].Value);