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

 

posted on 2009-07-19 03:32  阿C's  阅读(445)  评论(0编辑  收藏  举报