C#:如果datagrid单元格值是数字,则启用按钮

时间:2012-06-07 13:27:47

标签: c# visual-studio-2010 datagridview

这是Microsoft网站的功能,只在数据网格中输入数字,它工作正常。

如果第一行的第一个单元格值是数字,我想启用一个按钮???我怎么能这样做?

 private void dataGridView1_CellValidating(object sender,
    DataGridViewCellValidatingEventArgs e)
        {
            ThermalDatGrid.Rows[e.RowIndex].ErrorText = "";
            int newInteger;

            // Don't try to validate the 'new row' until finished 
            // editing since there
            // is not any point in validating its initial value.
            if (ThermalDatGrid.Rows[e.RowIndex].IsNewRow) {
                return;
            }
            if (!int.TryParse(e.FormattedValue.ToString(),
                out newInteger) || newInteger < 0)
            {
                e.Cancel = true;
                ThermalDatGrid.Rows[e.RowIndex].ErrorText = "Only Numerical and Non Negative Values";
            }
        }

1 个答案:

答案 0 :(得分:2)

尝试:

if (!int.TryParse(e.FormattedValue.ToString(),
             out newInteger) || newInteger < 0)
{
   e.Cancel = true;
   ThermalDatGrid.Rows[e.RowIndex].ErrorText = "Only Numerical and Non Negative Values";
}
else
{
   yourButton.Enabled=true;
}