DataGridView验证输入
实现Datagridview控件的CellValidating事件,在其中添加对内容的判断处理,设置row或Cell的ErrText属性.
--------------------------------------------------------
private void dataGridView1_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
{
double newSL = 0.0;
dataGridView1.Rows[e.RowIndex].ErrorText = string.Empty;
if(dataGridView1.Columns[e.ColumnIndex].DataPropertyName == "dhsl")
{
if(!double.TryParse(e.FormattedValue.ToString(),out newSL) || newSL<0.0)
{
dataGridView1.Rows[e.RowIndex].ErrorText = "数量输入有误!";
e.Cancel = true;
}
}
}
private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
dataGridView1.Rows[e.RowIndex].ErrorText = string.Empty;
}
--------------------------------------------------------
private void dataGridView1_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
{
double newSL = 0.0;
dataGridView1.Rows[e.RowIndex].ErrorText = string.Empty;
if(dataGridView1.Columns[e.ColumnIndex].DataPropertyName == "dhsl")
{
if(!double.TryParse(e.FormattedValue.ToString(),out newSL) || newSL<0.0)
{
dataGridView1.Rows[e.RowIndex].ErrorText = "数量输入有误!";
e.Cancel = true;
}
}
}
private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
dataGridView1.Rows[e.RowIndex].ErrorText = string.Empty;
}
加油,哥们,现在开始!