2016-12-06 2 views
-4

enter image description hereは、私がカテゴリーに基づいて行をループしたいボタンクリックイベントでのDataGridView

から複数の値を比較します。また、Datagridviewに一度にFrame、Lens、Consultationのカテゴリが含まれている場合。それから私は警告を表示したい。これを達成する方法は?私はforeachを試みたが結果はなかった。

foreach (DataGridViewRow row in dataGridViewInvoice.Rows) 
      { 
       string ItemType = row.Cells["SubCategory"].Value.ToString(); 
       if(ItemType == "Frame" && ItemType == "Lens" && ItemType == "Consultation") 
       { 
        MessageBox.Show("You can't select all items at once"); 
       } 
      } 
+2

[DataGridViewの内の各行をループ]の可能な重複(http://stackoverflow.com/questions/19737436/looping-each-row-in-datagridview) – Badiparmagi

+0

' ItemTypeは3つの値を同時に持つことはできません。 ** && **演算子を||に変更してください – Badiparmagi

答えて

1

すべてのレコードをループし、指定されたリスト内のアイテムのフラグを保持します。最後にメッセージを表示する場合、真のすべてのフラグ

bool frame,lens,type; 

foreach (DataGridViewRow row in dataGridViewInvoice.Rows) 
{ 
    string ItemType = row.Cells["SubCategory"].Value.ToString(); 
    if(ItemType == "Frame"){frame =true;} 
    else if (ItemType == "Lens"){lens =true;} 
    else if (ItemType == "Consultation"){type =true;} 
} 
if (frame && lens && type) 
{ 
     MessageBox.Show("You can't select all items at once"); 
} 
関連する問題