Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/14.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
vb.net中从datagridview将十进制数值保存为txt文件_Vb.net_Datagridview_Decimal_Text Files - Fatal编程技术网

vb.net中从datagridview将十进制数值保存为txt文件

vb.net中从datagridview将十进制数值保存为txt文件,vb.net,datagridview,decimal,text-files,Vb.net,Datagridview,Decimal,Text Files,这是我将值从数据网格视图保存到文本文件的代码: 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.Filte

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

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
Private Sub-TextFileToolStripMenuItem\u Click(发送方作为System.Object,e作为System.EventArgs)处理TextFileToolStripMenuItem。单击
Dim文件名为String=String.Empty
将sfd1变暗为新的SaveFileDialog()
sfd1.Filter=“txt文件(*.txt)|*.txt|所有文件(*.*)|*.”
sfd1.FilterIndex=2
sfd1.RestoreDirectory=True
sfd1.Title=“保存文本文件”
如果sfd1.ShowDialog()=DialogResult.OK,则
如果sfd1.FileName=String.Empty,则
MsgBox(“请输入文件名”)
其他的
filename=sfd1.filename.ToString
保存到_文本文件(dvList,文件名)
如果结束
如果结束
端接头
子保存到_TextFile(ByVal dvList作为DataGridView,ByVal filename作为字符串)
Dim numCols As Integer=dvList.ColumnCount-1
Dim numRows As Integer=dvList.RowCount
Dim strDestinationFile为字符串=“文件名和“.txt”
Dim tw As TextWriter=新的StreamWriter(StreamWriter)(strDestinationFile)
对于dvRow,整数=0到numRows-1
'检查复选框是否已选中,然后写入文本文件
如果dvList.Rows(dvRow).Cells.Item(0).Value=True,则
tw.写(“真”)
tw.写(“,”)
其他的
tw.写(“假”)
tw.写(“,”)
如果结束
'在文本文件中写入其余行
对于dvCol,作为整数=1到numCols
tw.Write(dvList.Rows(dvRow).Cells(dvCol.Value)
如果(dvCol numCols),则
tw.写(“,”)
如果结束
下一个
tw.WriteLine()
下一个
tw.Close()
端接头
这段代码工作得很好,但我唯一关心的是,我将数据网格视图的属性设置为带2位小数的
Numeric
。当我将其保存到文本文件时,它会删除小数点


如何在文本文件中保留小数位?

我修改了您的
保存到\u TextFile
方法。我在dvList中添加了两列[Column1]和[Column2]。我成功地保存了在[Column2]中输入的十进制值

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

如果使用格式,我会将数值列的行cellstyle设置为:

dvList.Columns("Column2").DefaultCellStyle.Format = "N2"
保存到_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
Private Sub Saveto_TextFile(ByVal dvList作为DataGridView,ByVal filename作为字符串)
Dim numCols As Integer=dvList.ColumnCount-1
Dim numRows As Integer=dvList.RowCount
Dim strDestinationFile为字符串=“文件名和“.txt”
Dim tw As TextWriter=新的StreamWriter(StreamWriter)(strDestinationFile)
对于dvRow,整数=0到numRows-1
'检查复选框是否已选中,然后写入文本文件
如果dvList.Rows(dvRow.Cells(“Column1”).Value=True,则
tw.WriteLine(dvList.Rows(dvRow.Cells(“Column2”).Value)”Column2是列的名称。。。您也可以在此处使用索引
其他的
tw.WriteLine(“未选中”)
如果结束
'在文本文件中写入其余行
对于dvCol,作为整数=1到numCols
tw.WriteLine(dvList.Rows(dvRow.Cells)(dvCol.Value)
如果(dvCol numCols),则
双写线(“?”)
如果结束
下一个
tw.WriteLine()
下一个
tw.Close()
端接头

我不知道我们是否可以说代码工作得很好…:)您的文本文件中有输出吗?@Tim:您好,先生,是的,我正在从datagridview输出到txt文件,但txt文件中的数值没有小数,但在datagridview中,有小数:)@Matthew您如何设置datagridview的格式?你是通过编程还是使用用户interface@Alex:您好,先生,我已经知道我的问题了,只是我只保存没有小数点的数字,但是如果这些数字有小数点,它就起作用了,对不起,这是我的错。