C# 如何将DataGridView设置为不接受空值?

C# 如何将DataGridView设置为不接受空值?,c#,C#,我正在使用windows窗体c。任何人都知道如何将DataGridView设置为不接受空值。因为我不希望用户插入空值。谢谢您可以在代码中检查空单元格或空单元格的值: foreach (DataGridViewRow dataRow in this.yourDataGridView.Rows) { for (int index = 0; index < dataRow.Cells.Count; index++) { if (dataRow.Cells[index].Value

我正在使用windows窗体c。任何人都知道如何将
DataGridView
设置为不接受空值。因为我不希望用户插入空值。谢谢

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

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...
    }
  } 
}
foreach(此.yourDataGridView.Rows中的DataGridViewRow dataRow)
{
对于(int index=0;index
您可以检查代码中空或空单元格的值:

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...
    }
  } 
}
foreach(此.yourDataGridView.Rows中的DataGridViewRow dataRow)
{
对于(int index=0;index
您可以将CellValueChanged事件添加到dataGridView控件中

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

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

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

你能解释一下你迄今为止为实现这一目标所做的努力吗?此外,请在问题中包含包含DataGridView初始化的详细信息和代码。这些将有助于我们对您的问题给出更准确的答案。您能解释一下您迄今为止为实现这一目标所做的努力吗?此外,请在问题中包含包含DataGridView初始化的详细信息和代码。这些将帮助我们对您的问题给出更准确的答案。