Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/324.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# 验证at单元时发生FormatException错误_C#_Winforms_Double - Fatal编程技术网

C# 验证at单元时发生FormatException错误

C# 验证at单元时发生FormatException错误,c#,winforms,double,C#,Winforms,Double,当我将文件加载到datagrid时,会收到一条错误消息,FormatException error。 这里,我要做的是,比较两个单元格值,如果col index 2的值大于col index 3,那么显示一条错误消息 当我第一次读取文件时,我没有任何问题。我将在秒的时间内加载,我将收到上面的错误消息 我尝试使用Convert.ToInt32,int,但仍然收到相同的错误消息。我怎样才能解决这个问题 private void datagridview_CellValidating(object s

当我将文件加载到datagrid时,会收到一条错误消息,
FormatException error
。 这里,我要做的是,比较两个单元格值,如果col index 2的值大于col index 3,那么显示一条错误消息

当我第一次读取文件时,我没有任何问题。我将在
秒的时间内加载,我将收到上面的错误消息

我尝试使用
Convert.ToInt32,int
,但仍然收到相同的错误消息。我怎样才能解决这个问题

private void datagridview_CellValidating(object sender, CellValidatingEventArgs e)
{
if (e.ColumnIndex != 0)
{
    if (e.RowIndex >= 0 && e.RowIndex < 8)
    {
        if (e.Value != null && datagridview.Rows[e.RowIndex].Cells[e.ColumnIndex - 1].Value != null)
        {
                //Convert.ToInt32, int////
            if (Double.Parse(e.Value.ToString()) <=
                Double.Parse(datagridview.Rows[e.RowIndex].Cells[e.ColumnIndex - 1].Value.ToString()))
            {
                MessageBox.Show("error");
                e.Cancel = true;
                datagridview.Rows[e.RowIndex].ErrorText = errorMesssage;
            }
        }
    }
}
}
private void datagridview\u CellValidating(对象发送方,CellValidatingEventArgs e)
{
如果(例如,ColumnIndex!=0)
{
如果(e.RowIndex>=0&&e.RowIndex<8)
{
if(e.Value!=null&&datagridview.Rows[e.RowIndex].Cells[e.ColumnIndex-1].Value!=null)
{
//Convert.ToInt32,int////

如果(Double.Parse(e.Value.ToString())我认为您应该在这里重新检查您的条件

if (Double.Parse(e.Value.ToString()) <=    
    Double.Parse(datagridview.Rows[e.RowIndex].Cells[e.ColumnIndex - 1].Value.ToString()))

if(Double.Parse(e.Value.ToString())解析时看到的值是什么?可能检查的值是无法解析的
Column1
(因为您有
单元格[e.ColumnIndex-1]
)e.Value中应该是什么数据类型?您正在读取什么类型的文件?我正在读取一个包含一些大整型值的Xml文件。我不明白为什么会发生这种情况。如果我第二次加载,我会收到上面的错误消息。我尝试了您的代码,仍然存在相同的问题。请您再给我解释一下。@linguini我的意思是如果你的第一列有一个无法解析的值,比如“Apple”,那么当你解析它时,它会抛出一个格式异常,因此你的整数值只在2和3列,而不是第一列,也许你可以设置一个值的示例布局。我真傻。如果(e.ColumnIndex!=0&&e.ColumnIndex!=2&&e.ColumnIndex!=3)
它可以工作。