Solved

URGENT:  Print DataGrid results to a text file

Posted on 2004-10-12
5
192 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:Tom Knowlton
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 10

Accepted Solution

by:
Hans Langer earned 500 total points
ID: 12293573
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:Tom Knowlton
ID: 12293658
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
ID: 12293897
You can read the file, format it and print it..
0
 
LVL 5

Author Comment

by:Tom Knowlton
ID: 12299126
Do you have sample code?
0
 
LVL 10

Expert Comment

by:Hans Langer
ID: 12299248
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

738 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