简体   繁体   English

从DataGrid读取时出现错误“对象引用未设置为对象的实例”

[英]Error “Object Reference not set to an Instance of an object” on read from DataGrid

Ok so I just did a quick test to see if my code was set to read properly from the DataGrid, and it does, but once it finishes reading I get the error "Object reference not set to an Instance of an object" and I'm not sure what it's referring to. 好的,所以我做了一个快速测试,以查看我的代码是否已设置为可以从DataGrid正确读取,并且可以,但是一旦完成读取,我将收到错误消息"Object reference not set to an Instance of an object"并且我不确定它指的是什么。 Is it because the loop continues after the rows has reached the end of the DataGrid? 是因为rows到达DataGrid的末尾之后循环仍继续吗?

public static void UploadFromExtrernalSource(PlantAreaCode_CreateView PACCreate)
{
    // For each row in the DataGrid, and for each column, stores the information in a string.
    for (int rows = 0; rows < PACCreate.dataGridView1.Rows.Count; rows++)
    {
        for (int col = 0; col < PACCreate.dataGridView1.Rows[rows].Cells.Count; col++)
        {
            string value = PACCreate.dataGridView1.Rows[rows].Cells[col].Value.ToString();
            Console.WriteLine(value + ",");
        }
    }
}

EDIT: Also when I print to console, it prints each value on a new line. 编辑:同样,当我打印到控制台时,它会在新行上打印每个值。 Why? 为什么?

Problem1 : You are trying to convert the null value from one of the cells into String . 问题1:您正在尝试将其中一个单元格的null值转换为String

Solution1 : before converting cell value into String do a null check. 解决方案1:在将单元格值转换为String之前,请先进行空检查。

Problem 2: you are using Console.WriteLine() method to display each cell value.so that it prints each cell value in a new Row/Line . 问题2:您正在使用Console.WriteLine()方法显示每个单元格值,以便在新的Row/Line打印每个单元格值。

Solution 2: you need to use Console.Write() method instead of Console.WriteLine() to print cell values, and once after printing all cell values from a given row you need to use Console.WriteLine() to print new line. 解决方案2:您需要使用Console.Write()方法而不是Console.WriteLine()来打印单元格值,并且在打印了给定行中的所有单元格值之后,您需要使用Console.WriteLine()来打印新行。

Suggestion: you don't need to declare the string variable value each time in a for loop, so you can move the string variable declaration out of the loop. 建议:您不需要每次在for循环中声明字符串变量value ,因此可以将字符串变量声明移出循环。

Try This: 尝试这个:

public static void UploadFromExtrernalSource(PlantAreaCode_CreateView PACCreate)
{
    // For each row in the DataGrid, and for each column, stores the information in a string.
    String value = String.Empty;
    for (int rows = 0; rows < PACCreate.dataGridView1.Rows.Count; rows++)
    {
        for (int col = 0; col < PACCreate.dataGridView1.Rows[rows].Cells.Count; col++)
        {
            if(PACCreate.dataGridView1.Rows[rows].Cells[col].Value!=null)
            { 
              value=PACCreate.dataGridView1.Rows[rows].Cells[col].Value;
              Console.Write(value + ","); 
            }
            else 
            { value=String.Empty; }
        }
        Console.WriteLine();//it prints a new line 
    }
}

hey i think you are trying to convert null value of a cell into string . 嘿,我认为您正在尝试将单元格的空值转换为字符串。 try to check for the null before converting it to the string hope this will help ! 尝试在将null转换为字符串之前检查null,希望对您有所帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM