如何检查datagridview中的选定行是否为空(没有项目)C#

Ari*_*Ari 7 c# datagridview

我如何检查行的单元格中是否有数据,即不是空/空.

我一直在尝试以下方面:

        if (dgvClient.SelectedRows.Count > 0)
        {
            DataGridViewRow currentRow = dgvClient.SelectedRows[0];
            if (currentRow.Cells.ToString() != String.Empty)
            {
                //The code that will be here will open a form
            }
            else
            {
                MessageBox.Show("Select a non null row");
            }
        }
Run Code Online (Sandbox Code Playgroud)

但是,它似乎没有工作,我没有想法:/

感谢您的帮助,Ari

Eoi*_*ell 8

.Cells是一个DataGridViewCell对象的集合.

您需要遍历该集合并测试每个单元格以查看它是否具有值...

if (currentRow.Cells.Count > 0) 
{      
   bool rowIsEmpty = true;    

   foreach(DataGridViewCell cell in currentRow.Cells)    
   {
      if(cell.Value != null) 
      { 
          rowIsEmpty = false;
          break;
      }    
   }

   if(rowIsEmpty)
   {
       MessageBox.Show("Select a non null row"); 
   }
   else
   {
       //DoStuff
   }
}
Run Code Online (Sandbox Code Playgroud)