How to give fixed width in datagridview to save in text format

raghunath

New member
Joined
Oct 23, 2009
Messages
2
Programming Experience
Beginner
I have a datagridview and few testbox. User types in the details into the text box and when pressed enter it adds to the datagridview. Now the question is user will be allowed to save the rows into a text file but it in not saving in a proper format ie i am getting as:

Quantity Name Rate
--------------------
12 wers 30
2323 ertd 40

there is no correct space between values. But i want it to save like this

VB.NET:
Quantity     Name      Rate
-------------------------
12             wers        30
2323          ertd         40

This is the code i am currently using

VB.NET:
strTemp = "DC" & id
                Using theWriter As New System.IO.StreamWriter("c:\" & strTemp & ".txt")
                    theWriter.WriteLine("--------------Billing Software--------------")
                    theWriter.WriteLine()
                    theWriter.WriteLine("Billing Date:" & System.DateTime.Now.Date & "  Time: " & System.DateTime.Now.Hour & ":" & System.DateTime.Now.Minute)
                    theWriter.WriteLine()
                    theWriter.WriteLine("---------------------------------------------")
                    theWriter.WriteLine()
                    theWriter.WriteLine("Quantity   Item/Sub Code   Description   Rate")
                    theWriter.WriteLine()
                    theWriter.WriteLine("---------------------------------------------")
                    Dim space As String = " "
                    For Each currentrow As DataGridViewRow In Me.DataGridView1.Rows
                        ' The Cells are the Columns
                        For Each currentcolumn As DataGridViewCell In currentrow.Cells
                            
                            theWriter.Write(currentcolumn.Value & "   ")
                        Next
                        theWriter.WriteLine()
                    Next
                    theWriter.WriteLine("---------------------------------------------")
                    theWriter.Close()
                End Using
Any ideas in vb.net ??? plz help me out
 
read and learn about composite formatting. It looks much cleaner and gives many options
Note: I don't warrant that this compiles so you'll still have to engage brain

VB.NET:
Using theWriter As New System.IO.StreamWriter("c:\" & strTemp & ".txt")
                    theWriter.WriteLine("--------------Billing Software--------------")
                    theWriter.WriteLine()
                    theWriter.WriteLine("Billing Date:{0:yyyyMMdd}  Time:{0:HHmmss}", DateTime.Now)
                    theWriter.WriteLine()
                    theWriter.WriteLine("---------------------------------------------")
                    theWriter.WriteLine()
'write the headers, our columns should be 20, 25, 30 and 10 wide 
                    theWriter.WriteLine("{0,20}{1,25}{2,30}{3,10}", "Quantity","Item/Sub Code","Description","Rate")
                    theWriter.WriteLine()
                    theWriter.WriteLine("---------------------------------------------")

                    For Each currentrow As [B]DataRow [/B]In [B]DATATABLE_THE_DATAGRIDVIEW_IS_BOUND_TO.Rows[/B]
'write the data, our columns should be 20, 25, 30 and 10 wide 
'but note that the numbers are sometimes MINUS now to align the other way
                          theWriter.WriteLine(("{0,-20}{1,25}{2,30}{3,-10}", currentRow.ItemArray)
                    Next
                    theWriter.WriteLine("---------------------------------------------")
                    theWriter.Close()
                End Using
 
Back
Top