2016-12-06 126 views
-4

enter image description here比较来自Datagridview的多个值

关于按钮单击事件我想遍历基于类别的行。如果Datagridview一次包含带有框架,镜头和咨询的类别。然后我想显示一个警报。如何实现这一目标?我尝试过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"); 
}