Ari
Ari

Reputation: 1036

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

Upvotes: 8

Views: 38386

Answers (6)

Ashoke Kumar
Ashoke Kumar

Reputation: 1

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.

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

}}

Upvotes: 0

Eoin Campbell
Eoin Campbell

Reputation: 44268

.Cells is a collection of DataGridViewCell objects.

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

Upvotes: 8

Law
Law

Reputation: 369

Surprised no linq answer so here it is:

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. You just need to cast the cells and rows.

Upvotes: 2

Jnr
Jnr

Reputation: 1654

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
}

Upvotes: 3

Glory Raj
Glory Raj

Reputation: 17691

I think this can be done by handling DataGridView.RowEnter event. RowEnter event occurs when a row receives input focus but before it becomes the current row. 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;
 }

Upvotes: 1

cmj
cmj

Reputation: 107

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

Upvotes: 1

Related Questions