简体   繁体   English

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

[英]How to check if a selected row in a datagridview is empty(has no item) C#

How would i go about checking if a row's cells have data in them, ie not empty/null. 我如何检查行的单元格中是否有数据,即不是空/空。

I've been trying the following: 我一直在尝试以下方面:

        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");
            }
        }

However, it doesn't appear to be working, and I'm out of ideas :/ 但是,它似乎没有工作,我没有想法:/

Thanks for any help, Ari 感谢您的帮助,Ari

.Cells is a collection of DataGridViewCell objects. .CellsDataGridViewCell对象的集合。

You need to iterate through that collection & test each cell to see if it has a value... 您需要遍历该集合并测试每个单元格以查看它是否具有值...

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
   }
}

Another method to check if a new empty row is selected perhaps 另一种检查是否选择了新空行的方法

if(dataGridView.CurrentRow.Index == dataGridView.Rows.Count -1)
{
    //you selected a new row
}

Surprised no linq answer so here it is: 惊讶没有linq回答所以这里是:

if (dataGridView1.Rows.Cast<DataGridViewRow>()
      .Any(x => x.Cells.Cast<DataGridViewCell>()
      .Any(c => c.Value != null)))

DataGrid Rows are of DataCollection type which implements IEnumerable. DataGrid行是DataCollection类型,它实现IEnumerable。 You just need to cast the cells and rows. 你只需要投射单元格和行。

         for (int i = 0; i < dataGridView1.Rows.Count; i++)
            {
                for (int j = 0; j < dataGridView1.Columns.Count; j++)
                {
                    DataGridViewCell cell = dataGridView1[j, i];
                    object value = cell.Value;
                    if (value == string.Empty)
                    {
                             //do
                    }
                }
            }

I think this can be done by handling DataGridView.RowEnter event. 我认为这可以通过处理DataGridView.RowEnter事件来完成。 RowEnter event occurs when a row receives input focus but before it becomes the current row. 当行接收输入焦点但在它成为当前行之前,会发生RowEnter事件。 For example, move from one cell to another cell in a different row. 例如,从一个单元格移动到另一行中的另一个单元格。

Check each cell value: 检查每个单元格值:

 void dataGridView1_RowEnter(object sender, DataGridViewCellEventArgs e)
 {
     dataGridView1.Rows[e.RowIndex].ReadOnly = checkrow(dataGridView1.Rows[e.RowIndex]);
 }

 bool checkrow(DataGridViewRow testrow)
 {
        for (int i = 0; i < testrow.Cells.Count; i++)
        {
            if (testrow.Cells[i].Value != null)
            {
                // if datagridview is databound, you'd better check whether the cell value is string.Empty
                if (testrow.Cells[i].Value.ToString() != string.Empty)
                {
                    // if value of any cell is not null, this row need to be readonly
                    return true;
                }

                // if there is an unbound checkbox column, you may need to check whether the cell value is null or false(uncheck).
            }
        }

        // else false
        return false;
 }

I have the same problem to check if the current row is empty in C# windows form, I put the following logic and it works for me, you can also use it if you want to check all the values then place all cells values like 0,1,2,3... or create a dynamic function to check it. 我有同样的问题来检查C#窗体中当前行是否为空,我把以下逻辑对我有用,如果要检查所有值然后将所有单元格值放置为0,也可以使用它1,2,3 ...或创建一个动态函数来检查它。

 private void dataGridViewProduct_RowHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (dataGridViewProduct.Rows[e.RowIndex].Cells[0].Value.ToString()!= string.Empty)
            { //dowork

}}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM