不允许dataGridView中的列输入为空

private void dataGridView1_CellValidating(object sender,
    DataGridViewCellValidatingEventArgs e)
{
    // Validate the CompanyName entry by disallowing empty strings.
    if (dataGridView1.Columns[e.ColumnIndex].Name == "CompanyName")
    {
        if (String.IsNullOrEmpty(e.FormattedValue.ToString()))
        {
            dataGridView1.Rows[e.RowIndex].ErrorText =
                "Company Name must not be empty";
            e.Cancel = true;
        }
    }
}

void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
    // Clear the row error in case the user presses ESC.  
    dataGridView1.Rows[e.RowIndex].ErrorText = String.Empty;
}

 

转载于:https://www.cnblogs.com/acis_/archive/2009/07/19/1526365.html

你可能感兴趣的:(不允许dataGridView中的列输入为空)