String &引用;datagridviewcell类型的值无法转换为字符串;运动模拟
我试图从DataGridView中获取数据,并将数据保存到txt文件中,以便稍后将其加载到网格中。现在,它将保存文件,但由于错误而不会写入文件 “datagridviewcell类型的值无法转换为字符串”或 “mscorlib.dll中发生“System.FormatException”类型的未处理异常 其他信息:输入字符串的格式不正确。“ 我不知道该怎么办,也找不到解决办法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
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可能就是您想要的。可能是重复的,非常感谢!欢迎,很高兴我能帮助你