DataGridView某个单元格只输入小数

来源:互联网 发布:东北林业大学网络 编辑:程序博客网 时间:2024/05/22 06:11

private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
        {          
                e.CellStyle.BackColor = Color.Aqua;//设置编译时的颜       
        }

        private void dataGridView1_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
        {
            if (dataGridView1.Rows[e.RowIndex].IsNewRow) { return; }

            decimal newInteger;
            if (e.ColumnIndex == 4 || e.ColumnIndex == 6)
            {
                if (e.FormattedValue != null && e.FormattedValue.ToString().Length != 0)
                {
                    if (!decimal.TryParse(e.FormattedValue.ToString(), out newInteger) || newInteger < 0)
                    {
                        e.Cancel = true;
                        MessageBox.Show("请输入数字或小数","提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
                    }

                }
            }
        }