根据网格单元格数量将DataGridView写入TextFile重复行

时间:2018-11-28 10:22:15

标签: vb.net

我有一个例程,可以根据DataGridView的内容生成文本文件。

情况就是这样。 销售订单行[项目名称[[数量]

解决方案是为每个数量生成一个文本文件。目前,我的例程只为每个订单行生成一行,而不是每个数量。

例如,如果当前行为第6列。

Try

        Dim StrExport As String = ""

        For Each C As DataGridViewColumn In DataGridView1.Columns
            StrExport &= "" & C.HeaderText & ","
        Next
        StrExport = StrExport.Substring(0, StrExport.Length - 1)
        StrExport &= Environment.NewLine

        For Each R As DataGridViewRow In DataGridView1.Rows
            For Each C As DataGridViewCell In R.Cells
                If Not C.Value Is Nothing Then
                    StrExport &= "" & C.Value.ToString & ","
                Else
                    StrExport &= "" & "" & ","
                End If
            Next
            StrExport = StrExport.Substring(0, StrExport.Length - 1)
            StrExport &= Environment.NewLine
        Next

        Dim tw As IO.TextWriter = New IO.StreamWriter(SaveFileDialog1.FileName)
        tw.Write(StrExport)
        tw.Close()
        My.Computer.FileSystem.RenameFile(SaveFileDialog1.FileName, "FRLSTicket.txt")
    Catch ex As Exception
        MsgBox(ex.Message)
    End Try

    Try
        DataGridView1.Refresh()

    Catch ex As Exception

    End Try

示例行可能是

SalesID,QTY,CustomerName,ItemID,Name,LegColour

SO1234567,3,约翰·史密斯(John Smith),主席,舒适的蓝色椅子,胡桃木

0 个答案:

没有答案