Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/16.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
String &引用;datagridviewcell类型的值无法转换为字符串;运动模拟_String_Vb.net_Datagridview - Fatal编程技术网

String &引用;datagridviewcell类型的值无法转换为字符串;运动模拟

String &引用;datagridviewcell类型的值无法转换为字符串;运动模拟,string,vb.net,datagridview,String,Vb.net,Datagridview,我试图从DataGridView中获取数据,并将数据保存到txt文件中,以便稍后将其加载到网格中。现在,它将保存文件,但由于错误而不会写入文件 “datagridviewcell类型的值无法转换为字符串”或 “mscorlib.dll中发生“System.FormatException”类型的未处理异常 其他信息:输入字符串的格式不正确。“ 我不知道该怎么办,也找不到解决办法 Private Sub SaveButton_Click(sender As Object, e As EventArg

我试图从DataGridView中获取数据,并将数据保存到txt文件中,以便稍后将其加载到网格中。现在,它将保存文件,但由于错误而不会写入文件

“datagridviewcell类型的值无法转换为字符串”或

“mscorlib.dll中发生“System.FormatException”类型的未处理异常 其他信息:输入字符串的格式不正确。“

我不知道该怎么办,也找不到解决办法

Private Sub SaveButton_Click(sender As Object, e As EventArgs) Handles SaveButton.Click
    'SGD is my save file dialog box
    SGD.ShowDialog()
    Using writer As New StreamWriter(SGD.FileName)
        For Each row As DataGridViewRow In QAfield.Rows
            writer.WriteLine(row.Cells.Item("AgentColumn").ToString, row.Cells.Item("ScoreColumn"), row.Cells.Item("PassColumn"), row.Cells.Item("FailColumn"))

        Next
    End Using
End Sub

错误是因为您没有将
访问到
DataGridViewCell
而不是单元格本身

 row.Cells.Item("AgentColumn").Value.ToString()
此返回是
DataGridViewCell

 row.Cells.Item("AgentColumn")
当您将
ToString
添加到字符串中时,您试图获取此对象并将其转换为字符串,这将不起作用,并且您看到的确切错误

  row.Cells.Item("AgentColumn").ToString
您需要访问服务器本身

 row.Cells.Item("AgentColumn").Value.ToString()
另一方面,如果
为NULL,则在添加
到字符串
时会引发异常,因为不能将NULL强制转换为空字符串。在将其转换为字符串之前,我将检查此

 If row.Cells.Item("AgentColumn").Value IsNot DBNull.Value Then ....

row.Cells.Item(“AgentColumn”).Value.ToString可能就是您想要的。可能是重复的,非常感谢!欢迎,很高兴我能帮助你