Click here to Skip to main content
15,845,681 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello Great Minds !
I am new here. I need to help to create a notepad which has data from data table.
I Filled a Data Table dt from data Adapter and Export using string builder.
I set Every Column Position Using Padding to fix column width.
I am unable to set header on top and column repeat after eatch 70 lines.
my code are under. i will very obliged to you

What I have tried:

VB
Dim Dt As New DataTable
   Private ColumnPadding() As Integer = {5, 25, 15, 10, 10, 20, 20, 15, 15, 35, 10, 20}
   Private OutPutFile As String = IO.Path.Combine(Application.StartupPath, "Local.txt")
   Private Function HeaderText() As String
       Return "Id".PadRight(5) & _
              "UserName".PadRight(25) & _
              "Indent No".PadRight(15) & _
              "Village".PadRight(10) & _
              "Code".PadRight(10) & _
       "Grower Name".PadRight(20) & _
        "Father Name".PadRight(20) & _
         "Mobile".PadRight(15) & _
          "Item Code".PadRight(15) & _
           "Item Name".PadRight(35) & _
            "Qty.".PadRight(10) & _
             "Amount".PadRight(10)
   End Function
   Private Sub ExportDataForGenericPrinter()
       Dim Total As Integer = Dt.Compute("Sum(Amt)", String.Empty)
       'For Unique Value Count
       Dim TotalUser As Integer = Dt.AsDataView.ToTable(True, "Userid").Rows.Count
       Dim TotalIndent As Integer = Dt.Compute("Count(IndNo)", String.Empty)
       Dim sb As New System.Text.StringBuilder
       Dim Line As New String("="c, HeaderText.Length)
       'For First Row Line Write
       sb.AppendLine(Line)
       'For Write Header Text
       sb.AppendLine(HeaderText)
       Dim ColLine As New String("--", HeaderText.Length)
       'For Repeat Eatch Column Single Line
       sb.AppendLine(Line)
       Dim ColumValues As String = ""
       For Each row As DataRow In Dt.Rows
           For col As Integer = 0 To Dt.Columns.Count - 1
               ColumValues &= row(col).ToString.PadRight(ColumnPadding(col))
           Next
           sb.AppendLine(ColumValues)
           sb.AppendLine(ColLine)
           ColumValues = ""
       Next
       sb.AppendLine(Line)
       sb.AppendLine("Total Indent :-  " & TotalUser & vbTab & "Total Indent :- " & TotalIndent & vbTab & "Total Amount :- " & Total)
       sb.AppendLine(Line)
       IO.File.WriteAllText(OutPutFile, sb.ToString)
       Application.DoEvents()
       Process.Start(OutPutFile)
   End Sub
   Private Sub BtnOk_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnOk.Click
       Try
           GetDataIndent()
       Catch ex As Exception
           MessageBox.Show(ex.Message)
       End Try
   End Sub
Posted
Updated 18-Dec-22 8:54am
v2

1 solution

First, you're not creating a "notepad". Notepad is an app that edits text files. You're creating a text file in your code.

In your code, I don't see you tracking the number of lines you've written to a "page" anywhere.

This is not hard to do. You write the header on the page and reset your counter to 0. Then you do through the records and write each record to the file and incrementing the counter. Check the counter for the number of lines you want on the page. When you reach that value, write the footer, a FormFeed character, then, if you still have more rows, write the header again, reset the line counter for the page, then resume writing records, ...
 
Share this answer
 
Comments
Amitkumaryadava 18-Dec-22 15:20pm    
Sir, my apologies. I want to write notepad. can you edit my code for counting line and insert header column sir?
Dave Kreskowiak 18-Dec-22 15:23pm    
You're making your own version of Notepad?

No, I'm not going to write your code for you. You don't write my code for me, so there we are.
Amitkumaryadava 18-Dec-22 17:42pm    
Thank you I figure it out to add a Line
Dim Counter as integer=0
If Counter = 24 Then
sb.AppendLine(" ")
sb.AppendLine(Line)
sb.AppendLine(HeaderText())
sb.AppendLine(Line)
sb.AppendLine(ColumValues)
sb.AppendLine(ColLine)
Counter = 0
Else
sb.AppendLine(ColumValues)
sb.AppendLine(ColLine)
End If
ColumValues = ""
Next

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