Dear frens,

from your help I succeeded in creating data grid . Thank you for it.

Now i need to generate report from data grid.

my data grid code is as follows;

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Db1DataSet1.Clear()
        Dim conStr As String = "Provider=Microsoft.JET.OLEDB.4.0;data source=K:\Database\db1.mdb"
        Dim con As OleDb.OleDbConnection = New OleDb.OleDbConnection(conStr)
        Dim S As String
        Dim datagrid1 As New DataGrid
        S = "select * from add1 where ID='" & TextBox1.Text & "'"
        Dim da As OleDb.OleDbDataAdapter = New OleDb.OleDbDataAdapter(S, con)
        da.Fill(Db1DataSet1, "add1")
        datagrid1.DataSource = Db1DataSet1.DefaultViewManager
End Sub

Now I want to generate report from the data field available in data grid in the form :
(Salutation) (fname) (mname) (lname), (degree)
(Position)
(organisation)
(Street) (City)
(State)
(Country)
Phone:(tel1) ,(tel2),(tel3)
Fax: (fax)
Email: (email)
URL: (URL)
I want to make two columns in the report to show various contacts' addresses in the data grid into printable form by generating report.

I don have any idea to generate report as i have never done it .

Please provide me with help with necessary explanations.

Anticipating for your help

Regards;
Kshiteesh

Dani AI

Generated

Two practical ways to get printable addresses out of that DataGrid: (A) produce a proper report (Crystal Reports or an RDLC/ReportViewer) and bind a prepared DataSet, or (B) build a custom print layout (PrintDocument) from the grid. ’s Crystal suggestion is perfectly valid; ’s DataGridView question can be solved either by printing the grid or by feeding the same prepared dataset into a report.

A reliable first step is to prepare presentation fields in the DataTable so the report only needs one textbox per logical line (no manual spacing of separate textboxes). Add a computed column like FullName and Phones (either with a DataColumn.Expression or in code) so empty parts are skipped and commas/linebreaks are only added when needed. Example (VB.NET):

' dt is the DataTable you filled from Access
If Not dt.Columns.Contains("FullName") Then dt.Columns.Add("FullName", GetType(String))
If Not dt.Columns.Contains("Phones") Then dt.Columns.Add("Phones", GetType(String))
For Each r As DataRow In dt.Rows
  Dim nameParts As New List(Of String)
  If r("Salutation") IsNot DBNull.Value AndAlso r("Salutation").ToString().Trim() <> "" Then nameParts.Add(r("Salutation").ToString().Trim())
  nameParts.AddRange({r("fname").ToString().Trim(), r("mname").ToString().Trim(), r("lname").ToString().Trim()}.Where(Function(s) s <> ""))
  Dim degree = r("degree").ToString().Trim()
  r("FullName") = String.Join(" ", nameParts).Trim() & If(degree = "", "", ", " & degree)
  r("Phones") = String.Join(", ", {"tel1","tel2","tel3"}.Select(Function(c) If(r(c) Is DBNull.Value, "", r(c).ToString().Trim())).Where(Function(s) s <> ""))
Next

(Also possible with DataColumn.Expression on the DataTable.) ()

If using Crystal Reports, put the combined fields or a single formula into one text object, set the field’s Format -> Common -> Can Grow, and eliminate empty lines with “Suppress embedded field blank lines” / “Suppress blank section.” For two-column label-style output use the Section Expert -> Details -> Format with Multiple Columns, set column width/gap and printing direction. (manualzilla.com)

If using RDLC/ReportViewer, either add the computed columns to the dataset or use an expression that conditionally concatenates fields (check IsNothing before concatenating). Allow the text box to grow vertically (Allow height to increase / CanGrow) and set two columns in the report page setup (Report Properties / Page Setup -> Columns) for newsletter-style two-column output. (learn.microsoft.com)

If a quick print from the grid is needed before a report is finished, use PrintDocument (handle PrintPage) or DrawToBitmap for a simple screenshot-style print; for nicely formatted, repeatable output prefer a report (RDLC or Crystal). The PrintDocument approach is the standard .NET printing route. (learn.microsoft.com)

Troubleshooting tips: always trim and null-check fields before joining; test with rows that have missing middle name, degree, or phones to confirm no stray commas or blank lines appear; preview PDF output to validate column widths before printing.

hai,
first check out -crystal report is installed in ur system& if its yes-then connect crystal report with db(by clicking on source option-present in file,menu)
And also manually connect db with reports-by writing code invb.net....i think after that,u should b able todo!!!!

Ya. thanks Pretham. saroja

I did as u said. But I have a problem while designing a report. I have to put different text fields in a format however the values have variable lengths.

I found trouble in managing the gaps between the fields. Is there any process or solution to make such that the values autoaccomodate the text boxes so that it looks like

[Salutation] [fname] [mname] [lname], [degree]
[Position]
[organisation]
[Street] [City] 
[State]
[Country]
Phone:[tel1] ,[tel2],[tel3]
Fax: [fax]
Email: [email]
URL: [url]

for example above format should show in report as;

Dr. Preetham Saroja, Phd(USA)
Executive Director
International Centre for Information Technology
16 Avenue Stree, Mery Land
12121WA, USA
Phone: 001923823283232, 0012832832382,001232323223
Fax: 00123237626322
Email: preetham.saroja@icit.org
URL: www.icit.org

But I have the difficulty , like when I design long name keeping in mind and use short name instead for eg: Ram instead of Gangottery. then
a problem exists the space between two field vaues broadens....

I m using VB.net version 8. I m using msaccess as bacend.

Please help me and provide me with necessary code if there is any. I have got just a week left for the deadline of my project.

Anticipating your reply

Kshiteesh

How can I generate report from data gridview control

How can I generate report from data gridview in vb.net.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.