如何设置DataGridView不接受空值?

时间:2016-01-17 20:27:01

标签: c#

我正在使用Windows表单c#。任何人都知道如何将 sample : http://localhost:8081/olshop/products/param/pants-m with paging http://localhost:8081/olshop/products/param/[category]-[gender]/paging sample : http://localhost:8081/olshop/products/param/pants-m/1 设置为不接受空值。因为我不希望用户插入空值。谢谢

2 个答案:

答案 0 :(得分:1)

您可以在代码中检查null或空的单元格值:

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

答案 1 :(得分:1)

您可以将CellValueChanged事件添加到dataGridView控件

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