Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/258.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# DataGridView–;弦乐;同一列/单元格中的小数_C#_String_Datagridview_Decimal - Fatal编程技术网

C# DataGridView–;弦乐;同一列/单元格中的小数

C# DataGridView–;弦乐;同一列/单元格中的小数,c#,string,datagridview,decimal,C#,String,Datagridview,Decimal,我在寻求建议 我有一个绑定的DataGridView,它有一个可为空的十进制列,并且刚刚被告知它还需要将“N/a”作为输入/输出。我现在的想法是隐藏绑定列并显示一个允许您输入十进制或“N/A”的输入列,并在单元格验证后使用-1.0表示“N/A”将数据写入隐藏绑定列。当我加载网格时,我会循环通过“隐藏”列,并将数据“复制/转换”到可见列。最坏的情况是大约200行,其中最多50/60行可见。 该项目仍处于开发/测试阶段,因此更改数据库中的列并不是最糟糕的事情 如果有更好的办法,我在听 谢谢。以下事件

我在寻求建议

我有一个绑定的DataGridView,它有一个可为空的十进制列,并且刚刚被告知它还需要将“N/a”作为输入/输出。我现在的想法是隐藏绑定列并显示一个允许您输入十进制或“N/A”的输入列,并在单元格验证后使用-1.0表示“N/A”将数据写入隐藏绑定列。当我加载网格时,我会循环通过“隐藏”列,并将数据“复制/转换”到可见列。最坏的情况是大约200行,其中最多50/60行可见。 该项目仍处于开发/测试阶段,因此更改数据库中的列并不是最糟糕的事情

如果有更好的办法,我在听


谢谢。

以下事件是我解决问题的方法

   private void grvRegrindRolls_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
   {
      if (StatusMode == FormStatusMode.Initialized && blnShown)
      {
         if (e.RowIndex >= 0)  //Not a new row
         {
            if (e.ColumnIndex == grcRRCenterAmountIn.Index)
            {
               if (grvRegrindRolls[grcRRCenterAmountIn.Index, e.RowIndex].Value != null
                  && ((string)grvRegrindRolls[grcRRCenterAmountIn.Index, e.RowIndex].Value.ToString().ToUpper() != "N/A")
                  && ((string)grvRegrindRolls[grcRRCenterAmountIn.Index, e.RowIndex].Value.ToString().ToUpper() != "NA")
                  && !IsValidDecimal((grvRegrindRolls[grcRRCenterAmountIn.Index, e.RowIndex].Value), false))
               {
                  grvRegrindRolls[grcRRCenterAmountIn.Index, e.RowIndex].ErrorText = INVALID;
               }
               else
                  grvRegrindRolls[grcRRCenterAmountIn.Index, e.RowIndex].ErrorText = NO_ERROR;
            }
         }
      }
   }

   private void grvRegrindRolls_CellValidated(object sender, DataGridViewCellEventArgs e)
   {
      if (StatusMode == FormStatusMode.Initialized && blnShown)
      {
         if (e.RowIndex >= 0 && e.ColumnIndex == grcRRCenterAmountIn.Index)  //Not a new row
         {
            if (grvRegrindRolls[grcRRCenterAmountIn.Index, e.RowIndex].Value != null
               && ((string)grvRegrindRolls[grcRRCenterAmountIn.Index, e.RowIndex].Value.ToString().ToUpper() != "N/A")
               && ((string)grvRegrindRolls[grcRRCenterAmountIn.Index, e.RowIndex].Value.ToString().ToUpper() != "NA")
               && !IsValidDecimal((grvRegrindRolls[grcRRCenterAmountIn.Index, e.RowIndex].Value), false))
            {
               grvRegrindRolls[grcRRCenterAmountIn.Index, e.RowIndex].ErrorText = INVALID;
            }
            else
            {
               if (grvRegrindRolls[grcRRCenterAmountIn.Index, e.RowIndex].Value != null
                  && IsValidDecimal(grvRegrindRolls[grcRRCenterAmountIn.Index, e.RowIndex].Value, false))
               {
                  grvRegrindRolls[grcRRCenterAmount.Index, e.RowIndex].Value = grvRegrindRolls[grcRRCenterAmountIn.Index, e.RowIndex].Value;
               }
               else if (grvRegrindRolls[grcRRCenterAmountIn.Index, e.RowIndex].Value != null
                  && ((grvRegrindRolls[grcRRCenterAmountIn.Index, e.RowIndex].Value.ToString().ToUpper() == "N/A")
                  || (grvRegrindRolls[grcRRCenterAmountIn.Index, e.RowIndex].Value.ToString().ToUpper() == "NA")))
               {
                  grvRegrindRolls[grcRRCenterAmount.Index, e.RowIndex].Value = "-1.000";
               }
               else if (grvRegrindRolls[grcRRCenterAmountIn.Index, e.RowIndex].Value == null || grvRegrindRolls[grcRRCenterAmountIn.Index, e.RowIndex].Value.ToString().Trim() == "")
               {
                  grvRegrindRolls[grcRRCenterAmount.Index, e.RowIndex].Value = null;
               }

               grvRegrindRolls[grcRRCenterAmountIn.Index, e.RowIndex].ErrorText = NO_ERROR;
            }
         }
      }
   }

   private void grvRegrindRolls_DataError(object sender, DataGridViewDataErrorEventArgs e)
   {
      //Uncancel the cell to allow the user to leave it
      e.Cancel = false;
   }

我不得不问,如果你想允许用户输入“N/A”…那么…你是否也希望允许用户输入“其他”文本,比如“hello”?不,如果不是小数点,就输入“N/A”。我允许N/A的变体:na、N/A、na、N/A,它们都显示为“N/A”,并保存为-1.0。如果您键入任何其他内容,您将得到一个错误标志/图标,不允许保存。然后,您必须在用户在单元格中键入时“捕获”他们的击键。显然,该列必须是“文本”列,这将允许用户键入任何字符。仅“捕获”数字值和单个小数点并不困难。但是,当您还希望允许用户键入这些字符时,可能会以“123N23”或“456A/N”或“AAA”或“/”等结尾。这是可行的,但这并非微不足道。一个只允许数值和一个小数点的简单解决方案听起来是一个更好的方法。如果该单元格为空,用户“离开”该单元格,则该单元格将显示“N/A”,并将-1设置为隐藏列。区别在于用户将无法在“不适用”中“键入”。要获得“N/A”,只需删除单元格中的文本,然后离开单元格即可。代码将添加“N/A”。我使用CellValidating()事件检查“N/A”输入或有效小数。我还检查了null/blank,因为他们可能还不想输入数据。我确实使用了CellValidating()&CellValidated()事件并复制了我在原始帖子中所述的数据。我在发帖寻求更好的想法。我不捕捉击键,但在键入完成后进行检查。