Click here to Skip to main content
16,021,004 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
am trying to export contents from datagrid to a text file and align them into their their proper columns...i have been able to write the contents of a datagrid to the textfile.....but my greatest problem is how to FORMAT the text to align them properly into their columns as it appeared on the datagrid view....please help...,

this is HOW the text file looked like :

Drawing       	0       	1596       
Capital       	1435       	0       
Sales       	0       	20607       
Purchaes       	15486       	0       
Purchases resturn       	0       	201       
Stock at 1 Jan       	2107       	0       
Carriage on purchases       	105       	0       
carriage on sales       	188       	0       
rent and rate       	460       	0 


THIS IS THE CODE I used::::

USIng objectwriter=io.file.append("c:/er.text")
For Each row As DataGridViewRow In Me.DataGridView1.Rows
                    If Not row.IsNewRow Then
                        Dim line As String = String.Empty
                        For x As Integer = 0 To row.Cells.Count - 1
           line &= row.Cells(x).Value &"     " vbTab
                        Next
                        objectwriter.WriteLine(line.Remove(line.Length - 1, 1))
                    End If
                Next
end using

PLEASE HELP:
how CAN I FORMAT the text file to align the contents of the text into proper row and columns as they appeared on the datagrid view thanks In advance!!!

[edit]Code block added - OriginalGriff[/edit]
Posted
Updated 17-Jun-11 23:40pm
v2

1 solution

You either need to find the longest text string, and the biggest number in each column, so you can allow space in all the others, or use a fixed width for each field.
You can then use a string Format in your WriteLine function to output fixed width fields:
For example:
Console.WriteLine("{0,-40}:{1,6}", "Hello", 167)
Would print
Hello                                   :   167
 
Share this answer
 
Comments
TugBest 18-Jun-11 7:10am    
thank u very much for your code...but the problem still exist.. it did not arrange the text contents into rows and columns like in the datagridview..please help me if u have another answer to my problem.. thanks once again
OriginalGriff 18-Jun-11 8:36am    
And what code did you try?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900