Solved

URGENT:  Print DataGrid results to a text file

Posted on 2004-10-12
5
188 Views
Last Modified: 2010-04-15
Is there an easy way to export the contents of a data grid to a text file?

Even better to print the contents directly to the printer....but if I can just get the contents out to a file I can print the text file myself.

Tom
0
Comment
Question by:knowlton
  • 3
  • 2
5 Comments
 
LVL 10

Accepted Solution

by:
Hans Langer earned 500 total points
Comment Utility
Maybe That Help:

you can change sw.Write(","); with the separator char,


private void OnExportGridToCSV(object sender, System.EventArgs e)
{
      // Create the CSV file to which grid data will be exported.
      StreamWriter sw = new StreamWriter(Server.MapPath("~/GridData.txt"), false);
      // First we will write the headers.
      DataTable dt = m_dsProducts.Tables[0];
      int iColCount = dt.Columns.Count;
      for(int i = 0; i < iColCount; i++)
      {
            sw.Write(dt.Columns[i]);
            if (i < iColCount - 1)
            {
                  sw.Write(",");
            }
      }
      sw.Write(sw.NewLine);
      // Now write all the rows.
      foreach (DataRow dr in dt.Rows)
      {
            for (int i = 0; i < iColCount; i++)
            {
                  if (!Convert.IsDBNull(dr[i]))
                  {
                        sw.Write(dr[i].ToString());
                  }
                  if ( i < iColCount - 1)
                  {
                        sw.Write(",");
                  }
            }
            sw.Write(sw.NewLine);
      }
      sw.Close();
}

Source: http://www.netomatix.com/DataGridSeries.aspx
GL
0
 
LVL 5

Author Comment

by:knowlton
Comment Utility
This exports the contents.

The points are yours.....but is there a way to (simply) print the exported file?
0
 
LVL 10

Expert Comment

by:Hans Langer
Comment Utility
You can read the file, format it and print it..
0
 
LVL 5

Author Comment

by:knowlton
Comment Utility
Do you have sample code?
0
 
LVL 10

Expert Comment

by:Hans Langer
Comment Utility
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Article by: Ivo
Anonymous Types in C# by Ivo Stoykov Anonymous Types are useful when  we do not need to follow usual work-flow -- creating object of some type, assign some read-only values and then doing something with them. Instead we can encapsulate this read…
In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now