2014-11-21 53 views
0

我试图通过相应列的值类型来验证datagridview的输入,这是我的代码:如何验证c#中列值类型的datagridview输入?

private void dgvLoadTable_CellValidating(object sender, DataGridViewCellValidatingEventArgs e) 
{ 
    foreach (DataGridViewRow dr in dgvLoadTable.Rows) 
    { 
    foreach (DataGridViewCell dc in dr.Cells) 
    { 
     if (dc.GetType() == typeof(Int32) || dc.GetType() == typeof(Double))//if(dc.ColumnIndex==0) 
     { 
     DataGridViewTextBoxCell cell = dgvLoadTable[e.ColumnIndex, e.RowIndex] as DataGridViewTextBoxCell; 
     if (cell != null) 
     { 
     char[] chars = e.FormattedValue.ToString().ToCharArray(); 
     foreach (char c in chars) 
     { 
     if (char.IsDigit(c) == false) 
     { 
      MessageBox.Show("You have to enter digits only"); 
      e.Cancel = true; 
      break; 
     }} } 
      } 
     } 
     } 
    } 

但是这是行不通的,应该是什么的一些列的类型?仅供参考,这是一个winForm应用程序,不同的数据通过用户选择在运行时在datagridview中加载。所以列以前不知道。 datagridview绑定到实体模型。

回答

0

首先,这取决于源集合中的数据类型。 所以它可能是十进制或其他一些数值。 最好如果你在这个方法中为某个单元格(列)添加条件断点。

此外,我会避免迭代通过每行anc列,因为此方法被称为特定单元格。

因此,您可能希望通过使用e.RowIndex和e.ColumnIndex从数据源中提取单元格数据对象,而不是对行/单元格进行foreach,您可能希望通过e.RowIndex和e.ColumnIndex获取单元格的确切数据。

我SRC看起来是对象的列表:初始化这样

class TestData 
{ 
    public Decimal Value { get; set; } 
    public string Name { get; set; } 
} 

List<TestData> src = new List<TestData>(); 
src.Add(new TestData() { Name = "Test 1", Value = 10 }); 
src.Add(new TestData() { Name = "Test 2", Value = 20 }); 
dataGridView1.DataSource = src; 

而且我的测试,可以在这里完成:

private void dataGridView1_CellValidating(object sender, DataGridViewCellValidatingEventArgs e) 
    { 
     var row = dataGridView1.Rows[e.RowIndex]; 
     if (null != row) 
     { 
      var cell = row.Cells[e.ColumnIndex]; 
      if (null != cell) 
      { 
       object value = cell.Value; 
       if (null != value) 
       { 
        // Do your test here in combination with columnindex etc 
       } 
      } 
     } 
    } 
+0

当然有很多种尝试/检查类型的方法。取决于你想要测试的是什么, – SebSky 2014-11-21 14:10:44

+0

不能使用列索引或名称,因为哪个表将被加载到数据网格中是事先不知道的。我有10 +表,他们根据用户选择加载。 – Paradox 2014-11-21 14:35:53

+0

是的,但使用上面你正在验证某些单元格不是整个表...然后,您可以使用http://stackoverflow.com/a/894296/4123158检查值是否为“数字” – SebSky 2014-11-24 09:36:31

0
private void dataGridView1_CellValidating(object sender, DataGridViewCellValidatingEventArgs e) 
    { 
     foreach (DataGridViewRow dr in dataGridView1.Rows) 
     { 
      foreach (DataGridViewCell dc in dr.Cells) 
      { 
       int x; 
       double y; 
       if (int.TryParse(dc.Value.ToString(),out x) || double.TryParse(dc.Value.ToString(),out y)) 
       { 
        MessageBox.Show("You have to enter digits only"); 
       } 

      } 
     } 
    } 

我希望这可以帮助你。我建议你使用tryparse函数来查看输入的值是否是数字。您也可以将此函数用于DataGridView_CellValueChanged函数。

+0

这是给我例外 - 运行程序时,对象引用未设置为对象的实例。 – Paradox 2014-11-21 14:38:02

0

对不起,关于最后一个,这是一个新的代码块。我试过它的工作。

private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e) 
    { 
     int x; 

     if (this.Visible && !int.TryParse(dataGridView1[e.ColumnIndex, e.RowIndex].Value.ToString(), out x)) 
     { 
      MessageBox.Show("You have to enter digits only"); 
      dataGridView1[e.ColumnIndex, e.RowIndex].Value = ""; 
     } 
    }