如何在 c# 中按列的值类型验证数据网格视图输入

本文关键字:数据 验证 类型 数据网 网格 输入 视图 | 更新日期: 2023-09-27 18:31:28

我正在尝试通过相应列的值类型验证数据网格视图输入,这是我的代码:

 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应用程序,不同的数据由用户选择在运行时加载到数据网格视图中。所以列以前是未知的。数据网格视图绑定到实体模型。

如何在 c# 中按列的值类型验证数据网格视图输入

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

此外,我会避免遍历每一行 anc 列,因为此方法是为特定单元格调用的。

因此,与其对行/单元格执行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
                }
            }
        }
    }
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功能。

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

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 = "";
        }
    }