How can I send a form and its Subform (Datagrid) as a text file?

How to I print a Form and its subform as a Text file

or send it to a text files then print the text file using a button

Maybe using something like this

Please be specific if you can. Thank you

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

'True escribe cada linea debajo en el file the texto "test.txt"./SIN borrar el anterior

' False, prepara un nuevo archivo en cada oportunidad

Dim objfile As New System.IO.StreamWriter("c:\test.txt", False)

'Estoy usando este formulario para escribir al formato de texto en el archivo test.txt

objfile.WriteLine("PENA'S ACCOUNTING")

objfile.WriteLine("17 Canal Street")

objfile.WriteLine("Salem, MA 01970")

objfile.WriteLine("(978) 745-8848")

objfile.WriteLine()

objfile.WriteLine()

objfile.WriteLine(Now())

objfile.WriteLine()

objfile.WriteLine("El Primer Numero es $" & Num1)

objfile.WriteLine()

objfile.WriteLine("_____________")

objfile.WriteLine()

objfile.WriteLine("La Segunda Cifra es: $ " & Num2)

objfile.WriteLine("==============")

objfile.WriteLine()

objfile.WriteLine("Resultado es $" & TextBox3.Text)

objfile.Close()

End Sub




Answer this question

How can I send a form and its Subform (Datagrid) as a text file?

  • VOC

    Hi,

    This article: "How to: Print a Multi-Page Text File in Windows Forms" (http://msdn2.microsoft.com/en-us/library/cwbe712d.aspx) shows how to print a text file on multiple pages. You can use the same code in this case. You can skip writing out to a text file, just use a StringBuilder to build up your stringToPrint variable.

    Hope this helps,



  • Dale Beyer

    Can you give an example

    Thanks.



  • Chris Honcoop

    well if your data is stored in the dataset then...

    theDataSet.WriteXml("path\myData.xml")

    and thats it! to load:

    theDataSet.LoadXml("path\myData.xml")



  • jankowiak

    Error : object file not declared

    how can I corrected



  • bhanoji

    Hi:

    I am looking for the second part; sending both the ParentForm and the Datagrid Subform to a text file.

    I understand how to send the parent form, but how do I send the DatagridSubform data to the text file

    Thanks, Fernando Pena



  • koko

    Step one: This is how I sent the Text files of the main form to the text file

    Step Two: How do I send the information of the Datagrid Subform also

    or send it to a text files then print the text file using a button

    Maybe using something like this

    Please be specific if you can. Thank you

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

    'True escribe cada linea debajo en el file the texto "test.txt"./SIN borrar el anterior

    ' False, prepara un nuevo archivo en cada oportunidad

    Dim objfile As New System.IO.StreamWriter("c:\test.txt", False)

    'Estoy usando este formulario para escribir al formato de texto en el archivo test.txt

    objfile.WriteLine("PENA'S ACCOUNTING")

    objfile.WriteLine("17 Canal Street")

    objfile.WriteLine("Salem, MA 01970")

    objfile.WriteLine("(978) 745-8848")

    objfile.WriteLine()

    objfile.WriteLine()

    objfile.WriteLine(Now())

    objfile.WriteLine()

    objfile.WriteLine("El Primer Numero es $" & Num1)

    objfile.WriteLine()

    objfile.WriteLine("_____________")

    objfile.WriteLine()

    objfile.WriteLine("La Segunda Cifra es: $ " & Num2)

    objfile.WriteLine("==============")

    objfile.WriteLine()

    objfile.WriteLine("Resultado es $" & TextBox3.Text)

    objfile.Close()

    End Sub



  • ShadowRayz

    you cant really. you would need to save the contents to a file. If you have databound a datagrid to a dataset then you can save the schema and data using the ReadXml/WriteXml methods in the dataset. Is this what you are after

    if you need to save textbox data etc... then you would need to write this to a textfile using a StreamWriter for example:

    Dim theWriter as new System.IO.StreamWriter("path\filename.txt")

    theWriter.WriteLine("string")

    theWriter.Close()



  • andrew_zep

    you would be looping through the datatable since that holds the data. I would suggest for you to write data to Xml since it will be much easier

  • NickBuck

    Hi,

    Make sure you declare the variable objFile like you did in your original code. The code I posted is a sample, not a fully working code. You need to adapt it into your program, not use it alone by itself.

    Best regards,



  • Darrin Turner

    How can I send Sending a form and its Subform (Datagrid) as a text file

  • sph3ra

    Is there a way to send every column of the datagrid to the text file using some kind of loop



  • datahook

    Assuming you're binding the DataGrid to a DataSet, you will need to print the data from the DataSet to the text file. For example this code will print all data tables, each row will be on one line separated by ";". You can apply other format to suite what you need

    Dim dataSet As DataSet = TryCast(Me.DataGrid1.DataSource, DataSet)
    If dataSet IsNot Nothing Then
    For Each dataTable As DataTable In dataSet.Tables
    objFile.WriteLine(
    "Table " & dataTable.TableName)
    Dim lineBuilder As New System.Text.StringBuilder
    For Each dataRow As DataRow In dataTable.Rows
    If lineBuilder.Length > 0 Then
    lineBuilder.Append(Environment.NewLine)
    End If
    For i As Integer = 0 To dataTable.Columns.Count - 1
    If i > 0 Then
    lineBuilder.Append(";")
    End If
    lineBuilder.Append(dataRow(i).ToString())
    Next
    Next
    objFile.WriteLine(lineBuilder.ToString())
    Next
    End If

    For printing directly to printer you can refer to this article http://www.dotnetcode.co.uk/howtoprintadatagrid.aspx

    Best regards,



  • GusBraga

    If you follow my code I am sending the information of the text fields to the the text file; But How do I send also the Subform which is in Datagrid format also to the text file. I imagine that the datagrid Subform has to be sent using a loop since one does not know how many lines will be there to be sent.

    Thanks



  • gafferuk

    Thank you very much.

    How would I be able to send it to a Text File



  • How can I send a form and its Subform (Datagrid) as a text file?