1.单元格只能输入整数
private void dataGridView1_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
{
if (e.ColumnIndex == 4)
{
if (dataGridView1.Rows[e.RowIndex].IsNewRow) { return; }
dataGridView1.Rows[e.RowIndex].ErrorText = "";
int NewVal = 0;
if (e.FormattedValue.ToString().Trim() != "") //该列也可以为空
{
try
{
int.Parse(e.FormattedValue.ToString()); //判断输入的是否为Int类型的数字
//!int.TryParse(e.FormattedValue.ToString(), out newInteger) || newInteger < 0 || String.IsNullOrEmpty(e.FormattedValue.ToString()) } catch (Exception ex) { e.Cancel = true; }
}
}
}
2.单元格只能输入小数和整数
private void dataGridView1_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
{
if (e.ColumnIndex == 5) 为列名
{
dataGridView1.Rows[e.RowIndex].ErrorText = "";
int NewVal = 0;
if (e.FormattedValue.ToString().Trim() != "") //该列也可以为空
{
try
{
Double.Parse(e.FormattedValue.ToString()); //判断输入的是否为double类型的数字
}
catch (Exception ex)
{
e.Cancel = true;
}
}
}
}
3.单击单元格 处于编辑状态
private void dataGridView1_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
{
if (e.ColumnIndex == 4)
{
try
{
dataGridView1.Rows[e.RowIndex].ErrorText = "";
this.dataGridView1.CurrentCell = this.dataGridView1[4, e.RowIndex];
this.dataGridView1.BeginEdit(true);
}
catch
{
MessageBox.Show("输入格式不正确"); //验证在一起使用
}
}
}
}
DataGridView中禁止排序
DataGridView.Columns(i).SortMode = DataGridViewColumnSortMode.NotSortable ;
private void dataGridView1_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
{
if (e.ColumnIndex == 4)
{
if (dataGridView1.Rows[e.RowIndex].IsNewRow) { return; }
dataGridView1.Rows[e.RowIndex].ErrorText = "";
int NewVal = 0;
if (e.FormattedValue.ToString().Trim() != "") //该列也可以为空
{
try
{
int.Parse(e.FormattedValue.ToString()); //判断输入的是否为Int类型的数字
//!int.TryParse(e.FormattedValue.ToString(), out newInteger) || newInteger < 0 || String.IsNullOrEmpty(e.FormattedValue.ToString()) } catch (Exception ex) { e.Cancel = true; }
}
}
}
2.单元格只能输入小数和整数
private void dataGridView1_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
{
if (e.ColumnIndex == 5) 为列名
{
dataGridView1.Rows[e.RowIndex].ErrorText = "";
int NewVal = 0;
if (e.FormattedValue.ToString().Trim() != "") //该列也可以为空
{
try
{
Double.Parse(e.FormattedValue.ToString()); //判断输入的是否为double类型的数字
}
catch (Exception ex)
{
e.Cancel = true;
}
}
}
}
3.单击单元格 处于编辑状态
private void dataGridView1_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
{
if (e.ColumnIndex == 4)
{
try
{
dataGridView1.Rows[e.RowIndex].ErrorText = "";
this.dataGridView1.CurrentCell = this.dataGridView1[4, e.RowIndex];
this.dataGridView1.BeginEdit(true);
}
catch
{
MessageBox.Show("输入格式不正确"); //验证在一起使用
}
}
}
}
DataGridView中禁止排序
DataGridView.Columns(i).SortMode = DataGridViewColumnSortMode.NotSortable ;
沒有留言:
張貼留言