使用 CheckBox 列将 CSV 导入 DataGridView 时出错
Importing CSV to DataGridView Error With CheckBox Column
我有一个 DataGridView,我将值保存到 CSV 文件中。该代码在 CSV 中创建一个空值,其中 DataGridView 中有一个空单元格。我遇到的问题是 DataGridView 中的第 10 列。这是一个 CheckBox 列。此列的值为“无”,除非已选中或未选中,在这种情况下,值变为 0 或 1(假或真)。
当我将 CSV 加载回 DataGridView 时,如果 CheckBox 列的值为空,我会收到错误消息。从 CSV 导入时,DataGridView 不接受此列的“无”值。
有没有办法将第 10 列的值仅导出为 0 或 1?
希望有人能帮我解决这个问题。
提前致谢。
这是我用来将 DataGridView 保存到 CSV 文件的代码。
Dim rows = From row As DataGridViewRow In DataGridView1.Rows.Cast(Of DataGridViewRow)()
Where Not row.IsNewRow
Select Array.ConvertAll(row.Cells.Cast(Of DataGridViewCell).ToArray, Function(c) If(c.Value IsNot Nothing, c.Value.ToString, ""))
Using sw As New IO.StreamWriter(Path)
For Each r In rows
sw.WriteLine(String.Join(",", r))
Next
End Using
这是我用来将 CSV 加载回 DataGridView 的代码。
Try
Dim streamReader As IO.StreamReader = New IO.StreamReader(FullPath)
'Read CSV file content
While streamReader.Peek() <> -1
rowvalue = streamReader.ReadLine()
cellvalue = rowvalue.Split(","c)
DataGridView1.Rows.Add(cellvalue)
End While
streamReader.Close()
Catch ex As Exception
MsgBox("File not found")
End Try
如果我很理解你需要这样的东西。这是您的代码,其中包含一些更改,可帮助您了解如何实现您的目标。
显然还有其他方法,其中之一是:
(请注意,它没有经过测试,它只是示范性的,你必须 work/test 它)
Dim indexOfCheckBoxColumn As Integer = 10
Dim getValues As Func(Of DataGridViewCell, String) = Function(c As DataGridViewCell)
If c.ColumnIndex = indexOfCheckBoxColumn Then
If c.Value Is Nothing Then Return "0"
Return CStr(IIf(Convert.ToBoolean(DirectCast(c, DataGridViewCheckBoxCell).Value) = True, "1", "0"))
Else
If c.Value IsNot Nothing Then Return c.Value.ToString
End If
Return ""
End Function
Dim rows = From row As DataGridViewRow In DataGridView1.Rows.Cast(Of DataGridViewRow)()
Where Not row.IsNewRow
Select Array.ConvertAll(row.Cells.Cast(Of DataGridViewCell).ToArray, Function(cell) getValues(cell))
Using sw As New IO.StreamWriter(Path)
For Each r In rows
sw.WriteLine(String.Join(",", r))
Next
End Using
如果您不希望该列中有任何空值,则不允许任何空值。最简单的选择是在添加行时将该列中单元格的 Value
设置为 False
,如果它尚未设置为其他内容:
Private Sub DataGridView1_RowsAdded(sender As Object, e As DataGridViewRowsAddedEventArgs) Handles DataGridView1.RowsAdded
Dim cell = DataGridView1.Rows(e.RowIndex).Cells(checkBoxColumnIndex)
Dim value = cell.Value
If value Is Nothing Then
cell.Value = False
End If
End Sub
因此,False
实际上成为该列的默认值。您现有的所有导出和导入代码都可以正常工作。
我有一个 DataGridView,我将值保存到 CSV 文件中。该代码在 CSV 中创建一个空值,其中 DataGridView 中有一个空单元格。我遇到的问题是 DataGridView 中的第 10 列。这是一个 CheckBox 列。此列的值为“无”,除非已选中或未选中,在这种情况下,值变为 0 或 1(假或真)。
当我将 CSV 加载回 DataGridView 时,如果 CheckBox 列的值为空,我会收到错误消息。从 CSV 导入时,DataGridView 不接受此列的“无”值。
有没有办法将第 10 列的值仅导出为 0 或 1?
希望有人能帮我解决这个问题。 提前致谢。
这是我用来将 DataGridView 保存到 CSV 文件的代码。
Dim rows = From row As DataGridViewRow In DataGridView1.Rows.Cast(Of DataGridViewRow)()
Where Not row.IsNewRow
Select Array.ConvertAll(row.Cells.Cast(Of DataGridViewCell).ToArray, Function(c) If(c.Value IsNot Nothing, c.Value.ToString, ""))
Using sw As New IO.StreamWriter(Path)
For Each r In rows
sw.WriteLine(String.Join(",", r))
Next
End Using
这是我用来将 CSV 加载回 DataGridView 的代码。
Try
Dim streamReader As IO.StreamReader = New IO.StreamReader(FullPath)
'Read CSV file content
While streamReader.Peek() <> -1
rowvalue = streamReader.ReadLine()
cellvalue = rowvalue.Split(","c)
DataGridView1.Rows.Add(cellvalue)
End While
streamReader.Close()
Catch ex As Exception
MsgBox("File not found")
End Try
如果我很理解你需要这样的东西。这是您的代码,其中包含一些更改,可帮助您了解如何实现您的目标。 显然还有其他方法,其中之一是: (请注意,它没有经过测试,它只是示范性的,你必须 work/test 它)
Dim indexOfCheckBoxColumn As Integer = 10
Dim getValues As Func(Of DataGridViewCell, String) = Function(c As DataGridViewCell)
If c.ColumnIndex = indexOfCheckBoxColumn Then
If c.Value Is Nothing Then Return "0"
Return CStr(IIf(Convert.ToBoolean(DirectCast(c, DataGridViewCheckBoxCell).Value) = True, "1", "0"))
Else
If c.Value IsNot Nothing Then Return c.Value.ToString
End If
Return ""
End Function
Dim rows = From row As DataGridViewRow In DataGridView1.Rows.Cast(Of DataGridViewRow)()
Where Not row.IsNewRow
Select Array.ConvertAll(row.Cells.Cast(Of DataGridViewCell).ToArray, Function(cell) getValues(cell))
Using sw As New IO.StreamWriter(Path)
For Each r In rows
sw.WriteLine(String.Join(",", r))
Next
End Using
如果您不希望该列中有任何空值,则不允许任何空值。最简单的选择是在添加行时将该列中单元格的 Value
设置为 False
,如果它尚未设置为其他内容:
Private Sub DataGridView1_RowsAdded(sender As Object, e As DataGridViewRowsAddedEventArgs) Handles DataGridView1.RowsAdded
Dim cell = DataGridView1.Rows(e.RowIndex).Cells(checkBoxColumnIndex)
Dim value = cell.Value
If value Is Nothing Then
cell.Value = False
End If
End Sub
因此,False
实际上成为该列的默认值。您现有的所有导出和导入代码都可以正常工作。