【问题标题】:Verify a Color Has Been Inputted in a DataGridView验证已在 DataGridView 中输入颜色
【发布时间】:2014-10-23 14:40:21
【问题描述】:

我有一个数据网格视图,其中一列是颜色列。我想确保用户输入有效的颜色,否则将单元格留空。

当单元格从空白开始时,以下代码给了我一个空引用异常。仅供参考,我在 CellLeave 活动中这样做。

Answer 我不得不将代码移至 CellFormatting 事件。由于某种原因,单元格的值直到某个未知点才会更新。对于我的检查,我需要在 CellFormatting 事件中执行某些操作之前执行此操作。将代码移到那里解决了我的问题。

private void dataGridView1_CellFormatting(object sender, DataGridViewCellEventArgs e)
{
    if (e.ColumnIndex.Equals(2))
    {
        Regex test = new Regex("[0-255],[0-255],[0-255]");

        Match m = test.Match(this.dataGridView1.CurrentCell.Value.ToString());

        if(!m.Success)
        {
            this.dataGridView1.CurrentCell.Value = "";
        }
    }
}

【问题讨论】:

  • 您考虑过使用颜色对话吗?
  • 我使用它作为用户选择颜色的第二种方式。我还希望他们能够输入他们选择的(数字)颜色。
  • 请看下面的my answer,我在其中展示了如何在 CellParsing 事件处理程序中正确更新单元格的值。

标签: c# .net regex string datagridview


【解决方案1】:

要验证字符串,您可以使用以下方法:

private static bool IsValidColorString(string input)
{
    if (String.IsNullOrEmpty(input))
        return false;
    string[] parts = input.Split(',');
    if (parts.Length != 3)
        return false;
    foreach (string part in parts)
    {
        int val;
        if (!int.TryParse(part, out val) || val < 0 || val > 255)
            return false;
    }
    return true;
}

我认为使用它的最佳位置是DataGridView.CellParsing 事件处理程序:

private void dataGridView1_CellParsing(object sender, DataGridViewCellParsingEventArgs e)
{
    if (e.Value == null || e.ColumnIndex != 2) // Skip empty cells and columns except #2
        return;
    string input = e.Value.ToString();
    if (!IsValidColorString(input))
        e.Value = String.Empty;  // An updated cells's value is set back to the `e.Value`
}

更新后的单元格的值设置回e.Value,而不是DataGridView.CurrentCell.Value

【讨论】:

    【解决方案2】:

    为你的比赛试试这个:

    Match m = test.Match((this.dataGridView1.CurrentCell.Value ?? "").ToString());
    

    这将在匹配时将空值替换为空字符串。

    【讨论】:

    • 我认为我遇到了不同的问题。该值似乎没有正确更新。
    【解决方案3】:

    在尝试匹配正则表达式之前,您是否可能需要检查单元格中是否有值?

    if (e.ColumnIndex.Equals(2))
    {
      if(this.dataGridView1.CurrentCell.Value != null)
      {
        ...
    

    【讨论】:

      【解决方案4】:

      CurrentCell.Value.ToString()

      导致错误..

      CurrentCell.value=null 
      

      null 值不能使用 tostring() 方法进行转换,这就是为什么你会得到 null 引用异常。

      试试这个..

      string val="";
          if(dataGridView1.CurrentCell.Value==null)
      {
      val=""
      }
      els
      else
      {
      val=convert.tostring(dataGridView1.CurrentCell.value);
      }
      Match m = test.Match(val);
       if(!m.Success)
              {
                  this.dataGridView1.CurrentCell.Value = "";
              }
      

      【讨论】:

      • 是的,我发现了。问题是,我在 CellParsing 事件中更改了单元格的值。但是,当我参加另一个活动时,单元格数据还没有改变。
      • @user2970916,请看下面我的回答,我在其中展示了如何正确更新CellParsing 事件处理程序中的单元格值。
      猜你喜欢
      • 2016-06-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-21
      • 2017-04-09
      • 2015-12-23
      • 2023-03-30
      相关资源
      最近更新 更多