Kate
Kate

Reputation: 945

How to set the DataGridView to not accept empty value?

I am using windows forms c# . Anyone knows how to set the DataGridView to not to accept empty value. because I do not want the user to insert empty values . Thank you

Upvotes: 0

Views: 347

Answers (2)

Almir_
Almir_

Reputation: 11

You can add CellValueChanged event to your dataGridView control

 private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
    {
        if (dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value == null)
        {
            //Do something
        }
    }

Upvotes: 1

Denis Bubnov
Denis Bubnov

Reputation: 2795

You can check value of cells on null or empty in your code:

foreach (DataGridViewRow dataRow in this.yourDataGridView.Rows)
{
  for (int index = 0; index < dataRow.Cells.Count; index++)
  {
    if (dataRow.Cells[index].Value == null || dataRow.Cells[index].Value == System.DBNull.Value || string.IsNullOrEmpty(dataRow.Cells[index].Value.ToString()))
    {
        // here is your logic...
    }
  } 
}

Upvotes: 1

Related Questions