可以在datagridview的dataGridView1_CellValidating事件中进行验证
private void dataGridView1_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
{
//假设要对第一列的数据进行验证
if (e.ColumnIndex == 0)
{
try
{
if (!String.IsNullOrEmpty(e.FormattedValue.ToString()))
{
decimal val = decimal.Parse(e.FormattedValue.ToString());
}
}
catch (Exception ex)
{
dataGridView1.Rows[e.RowIndex].ErrorText = "必须输入数字";
MessageBox.Show("请输入数字 ");
e.Cancel = true;
}
}
}