2016-10-31 6 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が返されるので、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); 
関連する問題