Solved

Extract DataGridView1 to text file No 2

Posted on 2011-09-24
2
174 Views
Last Modified: 2012-05-12
Hello again.

I'm openning a second topic with a similar subject, but with one detail more.

Extract DataGridView1 to text file

No, i can extract from a GridView to a text file.

But now, i need to also extract the grid to a text file this way:

From column 1 to 10 of the grid would be transfered to the text file on one row and from column 11 to 15 just under that row in the same text file..

And this, for every grid row.

Is that posible to do?

Dim xOut As New Dictionary(Of Integer, Integer) From {{0, 1}, {1, 1}, {2, 1}, {3, 12}, {4, 12}, {5, 1}, {6, 35}, {7, 35}, {8, 20}, {9, 1}, {10, 35}, {11, 35}, {12, 20}, {13, 8}, {14, 12}, {15, 5}}
Dim MyPath As String = "C:\Documents and Settings\All Users\Desktop\tl.02" & Format(Now, "yymmddhhmmss")
Dim xBuilder As New StringBuilder
For Each row As DataGridViewRow In Me.DataGridView1.Rows
    Dim xList As String = Nothing
    For x = 0 To row.Cells.Count - 1
        Dim xValue = row.Cells(x).Value.ToString
        If xValue.Length < xOut.Item(x) Then xValue = xValue.PadRight(xOut.Item(x))
        xBuilder.Append(xValue)
    Next
    xBuilder.Append(vbCrLf)
Next
File.WriteAllText(MyPath, xBuilder.ToString)

Open in new window



Thanks again for your help.



0
Comment
Question by:Wilder1626
2 Comments
 
LVL 17

Accepted Solution

by:
nepaluz earned 500 total points
ID: 36593376
just insert one line that enters a VbCrLf when x = 36 and thats it.
This assumes that the grid will only have 50 columns.
Dim xOut As New Dictionary(Of Integer, Integer) From {{0, 1}, {1, 1}, {2, 1}, {3, 12}, {4, 12}, {5, 1}, {6, 35}, {7, 35}, {8, 20}, {9, 1}, {10, 35}, {11, 35}, {12, 20}, {13, 8}, {14, 12}, {15, 5}}
Dim MyPath As String = "C:\Documents and Settings\All Users\Desktop\tl.02" & Format(Now, "yymmddhhmmss")
Dim xBuilder As New StringBuilder
For Each row As DataGridViewRow In Me.DataGridView1.Rows
    Dim xList As String = Nothing
    For x = 0 To row.Cells.Count - 1
        If x = 36 Then xBuilder.Append(vbCrLf)
        Dim xValue = row.Cells(x).Value.ToString
        If xValue.Length < xOut.Item(x) Then xValue = xValue.PadRight(xOut.Item(x))
        xBuilder.Append(xValue)
    Next
    xBuilder.Append(vbCrLf)
Next

Open in new window

File.WriteAllText(MyPath, xBuilder.ToString)
0
 
LVL 11

Author Closing Comment

by:Wilder1626
ID: 36593406
Thank you so much

This is perfect.
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now