0
votes

My VB.NET application has a datagridview (Named = "DGV") which have 4 columns. I want to write multiple text files with data grid rows. the name of text file should be the value of the cell of column 1, and the text of text file should be the value of column 3 cell. The attached image will clear every thing. Please Kindly help.These values are just examples and the actual values are totally differ from each other

2

2 Answers

1
votes

Here's one way. First i'm creating a DataTable from the DataGridView to simplify the task. Then i'm using Linq-To-DataTable to get what i need (dir is the path to your directory).

Dim table = New DataTable()
For i As Int32 = 0 To dgv.Columns.Count - 1
    table.Columns.Add(dgv.Columns(i).Name)
Next

For Each gvRow As DataGridViewRow In dgv.Rows
    Dim row = table.Rows.Add()
    For col As Int32 = 0 To dgv.Columns.Count - 1
        row(col) = gvRow.Cells(col).Value
    Next
Next

Dim fileInfos = From r In table
                Select New With {
                   .File = New FileInfo(Path.Combine(dir, r.Field(Of String)(0))),
                   .Content = r.Field(Of String)(2)
                }

Now create the text-files and write the text:

For Each fileInfo In fileInfos
    Try
        fileInfo.File.Create()
        Using writer = fileInfo.File.CreateText()
            writer.Write(fileInfo.Content)
        End Using
    Catch ex As Exception
        ' log etc.
    End Try
Next

I'm using .NET Version 2.0 and I cant change it

Here's the .NET 2 version:

For Each row As DataRow In table.Rows
    If Not row.IsNull(0) AndAlso row(0).ToString().Length <> 0 Then
        Dim path = IO.Path.Combine(dir, row(0).ToString())
        Dim file = New IO.FileInfo(path)
        Try
            file.Create()
            Using writer = file.CreateText()
                Dim content = ""
                If Not row.IsNull(2) Then content = row(2).ToString()
                writer.Write(content)
            End Using
        Catch ex As Exception
            ' log etc.
        End Try
    End If
Next
0
votes

I design this code and I wonder It's Working Perfectly. First of all Crate a boolean property at the top of your codes after "Public Class Form1"

Public Property SelectedIdexLock As Boolean = False

Now in Datagride1 Selection Changed:

If SelectedIdexLock = True Then
            My.Computer.FileSystem.WriteAllText("C:\Users\YOUR USER NAME HERE\Desktop\" & Datagride1.SelectedCells(0).Value.ToString & ".txt", Datagride1.SelectedCells(2).Value.ToString, False)
        End If

OK this is last step, In Test Button (Button1) Click:

        Dim RowsCount As Integer = Datagride1.Rows.Count.ToString
        Datagride1.Rows(0).Selected = True
        Datagride1.Rows(0).Selected = False

        For Each row In Datagride1DataSet.Table1.Rows

            Dim i As Integer
            For i = 0 To RowsCount
                If i = RowsCount Then
                    SelectedIdexLock = False
                    Exit Sub
                Else
                    SelectedIdexLock = True
                    Datagride1.Rows(i).Selected = True
                End If

            Next

        Next

You have done!