1

这是我将数据网格视图中的值保存到文本文件的代码:

Private Sub TextFileToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles TextFileToolStripMenuItem.Click
    Dim filename As String = String.Empty
    Dim sfd1 As New SaveFileDialog()

    sfd1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*"
    sfd1.FilterIndex = 2
    sfd1.RestoreDirectory = True
    sfd1.Title = "Save Text File"

    If sfd1.ShowDialog() = DialogResult.OK Then
        If sfd1.FileName = String.Empty Then
            MsgBox("Please input filename")
        Else
            filename = sfd1.FileName.ToString
            Saveto_TextFile(dvList, filename)
        End If
    End If
End Sub

Sub Saveto_TextFile(ByVal dvList As DataGridView, ByVal filename As String)
    Dim numCols As Integer = dvList.ColumnCount - 1
    Dim numRows As Integer = dvList.RowCount
    Dim strDestinationFile As String = "" & filename & ".txt"
    Dim tw As TextWriter = New StreamWriter(strDestinationFile)

    For dvRow As Integer = 0 To numRows - 1
        'checking if the checkbox is checked, then write to text file
        If dvList.Rows(dvRow).Cells.Item(0).Value = True Then
            tw.Write("True")
            tw.Write(", ")
        Else
            tw.Write("False")
            tw.Write(", ")
        End If

        'write the remaining rows in the text file
        For dvCol As Integer = 1 To numCols
            tw.Write(dvList.Rows(dvRow).Cells(dvCol).Value)
            If (dvCol <> numCols) Then
                tw.Write(", ")
            End If
        Next
        tw.WriteLine()
    Next
    tw.Close()
End Sub

此代码运行良好,但我唯一担心的是我将数据网格视图的属性设置为Numeric小数点后 2 位。当我将它保存到文本文件时,它会删除小数位。

我该怎么做才能保留文本文件中的小数位?

4

1 回答 1

2

我修改了你的SaveTo_TextFile方法。我在 dvList [Column1] 和 [Column2] 中添加了两列。我能够成功保存在 [Column2] 中输入的十进制值。

我不知道您如何格式化 DataGridView 列,但我的只是一个没有格式化的 DataGridViewTextBoxCell。

如果我使用格式,这就是我将数字列的行单元格样式设置为:

dvList.Columns("Column2").DefaultCellStyle.Format = "N2"

SaveTo_TextFile方法

Private Sub Saveto_TextFile(ByVal dvList As DataGridView, ByVal filename As String)
    Dim numCols As Integer = dvList.ColumnCount - 1
    Dim numRows As Integer = dvList.RowCount
    Dim strDestinationFile As String = "" & filename & ".txt"
    Dim tw As TextWriter = New StreamWriter(strDestinationFile)

    For dvRow As Integer = 0 To numRows - 1
        'checking if the checkbox is checked, then write to text file
        If dvList.Rows(dvRow).Cells("Column1").Value = True Then
            tw.WriteLine(dvList.Rows(dvRow).Cells("Column2").Value) 'Column2 is the name of the column ... You can also use an index here
        Else
            tw.WriteLine("Not Checked")
        End If

        'write the remaining rows in the text file
        For dvCol As Integer = 1 To numCols
            tw.WriteLine(dvList.Rows(dvRow).Cells(dvCol).Value)
            If (dvCol <> numCols) Then
                tw.WriteLine("???")
            End If
        Next
        tw.WriteLine()
    Next
    tw.Close()
End Sub
于 2013-09-17T01:52:50.210 回答