2017-07-24 72 views
0

如果行单元格具有此值(D),并且没有行具有像这样的单元格值,则需要搜索datagridview。这是我的代码,但它的错误如何检查datagridview是否返回具有特定值的0行

ID Qty Price D 
================== 
1 5 2.00 D 
2 4 3.00 
3 2 10.00 D 

这就是我的代码datagridview的示例。

private void noD() 
    { 
     foreach (DataGridViewRow row in dgvPOScart.Rows) 
     { 
      if (Convert.ToChar(row.Cells[3].Value) != 'D') 
      { 
       // If no rows withcell[3] having a value of D 
        // (For example only item is with ID 2) 
       //Do this 

      } 

     } 

    } 
+0

您的索引称其为6?例如reeplace row.Cells [3]。值 – CMedina

+0

是的,我的实际索引是6位右边我将它更改为3,例如ty – FutureDev

+1

只需使用布尔标志变量,如果您发现你的专栏中有一个“D”。 *在循环*之后,如果该标记为假,则没有折扣。如果这是真的,那么你确实有打折项目。 – LarsTech

回答

0

使用此代码管理解决此问题。根据先生LarsTech和先生CMedina的技巧。

private bool withD() 
    { 
     foreach (DataGridViewRow row in dgvPOScart.Rows) 
     { 
      if (row.Cells[3].Value.ToString().Equals("D")) 
      { 
       return true; 
      } 

     } 
     return false; 

    } 

if(withD()) 
     { 
      //code if a row with a cell value of "D" is found 
     } 
     else 
     { 
      //code if no cell with a value of "D" is found 
     } 
1

试试这个代码,它对我有用!

int allDValuecount = 0; 
int allOtherValueCount = 0; 
int rowsCount = DataGridViewRow.Rows.Cont; 
foreach(DataGridViewRow row in DataGridViewRow.Rows) 
{ 
    if(!row.Cells[3].Value.ToString().Equals("D")) 
    { 
     allOtherValueCount++; 
    }else 
    { 
     allDValuecount++; 
    } 
} 

if(allDValuecount == rowsCount){//code for all D values} 
if(allOtherValueCount == rowsCount){//code for other values} 
+0

感谢先生几乎正确,但仍然存在问题。如果代码发现1行值不为“D”,则此代码激活。我需要的是,它只会在所有行不是=“D”或者所有具有单元格[3]值的行都是=“”时激活。 – FutureDev

+1

@FutureDev检查我的编辑 – CMedina

+0

仍然不工作先生。我试着将3个变量转换为标签和变量“allDvaluecount”总是为零,“allothervaluecount”总是= rowcount。 – FutureDev

相关问题