Link to home
Start Free TrialLog in
Avatar of 1030071002
1030071002Flag for United States of America

asked on

How do I make a data grid in .Net and print a data grid

How do I make a data grid in .Net and print a data grid
Avatar of khan_webguru
khan_webguru
Flag of Australia image

Please see the code below step by step

Createin ASPX (.Net page)

 
<%@ Page Language = "C#" %>
<%@ Import Namespace = "System.Data" %>

<html>
  <head>
    <title>DataGrid Sample</title>
    
    
  </head>
    
  <body>
    <asp:DataGrid id = "myGrid"
                  CellPadding = "5"
                  AutoGenerateColumns = "false"
                  Font-Name = "verdana"
                  Font-Size = "10pt"
                  runat = "server">
                  
      <HeaderStyle BackColor = "#336699" 
                 ForeColor = "#ffffff" Font-Bold = "true" />      
      <AlternatingItemStyle BackColor = "#eeeeee" />
                  
      <Columns>
        <asp:BoundColumn HeaderText = "First Name" 
                            DataField = "firstName" />
        <asp:BoundColumn HeaderText = "Last Name" 
                            DataField = "lastName" />
        <asp:BoundColumn HeaderText = "Email" 
                            DataField = "emailAddress" />
      </Columns>
    </asp:DataGrid>
  </body>
</html>

Open in new window


See the code behind for binding that grid to some data

 
private void Page_Load(object sender, EventArgs e)
      {
        if (!Page.IsPostBack)
          BindGrid();
      }
      
      private void BindGrid()
      {
        DataTable table = new DataTable("Users");
        table.Columns.Add("firstName");
        table.Columns.Add("lastName");
        table.Columns.Add("emailAddress");
        
        AddRow(table, "Bugs", "Bunny", "bbunny@wb.com");
        AddRow(table, "Mickey", "Mouse", "mmouse@disney.com");
        AddRow(table, "Donald", "Duck", "dduck@disney.com");
        AddRow(table, "Dan", "Marino", "dmarino@dolphins.com");
        AddRow(table, "Steve", "Stchur", "sstchur@yahoo.com");
        
        myGrid.DataSource = new DataView(table);
        myGrid.DataBind();        
      }
      
      private void AddRow(DataTable table, string firstName, 
                          string lastName, string email)
      {
        DataRow row = table.NewRow();
        row["firstName"] = firstName;
        row["lastName"] = lastName;
        row["emailAddress"] = email;
        table.Rows.Add(row);
      }

Open in new window


Handling Print button

 
protected void OnPrint(object sender, EventArgs e)
{
    m_obPrtMgr = new PrintManager();

    PRT2DISKLib.Print2DiskClass pdfServer = new PRT2DISKLib.Print2DiskClass();
    String strPdfFile = Server.MapPath("~/PdfReports");
    pdfServer.PDFAuthor = "Winista";
    pdfServer.OutputDirectory = strPdfFile;
    pdfServer.NewDocumentName = Guid.NewGuid().ToString("N") + ".pdf";
    strPdfFile = System.IO.Path.Combine(strPdfFile, pdfServer.DocumentName);
    pdfServer.GeneralFlags += 16;

    PrintDocument doc = new PrintDocument();
    doc.PrintPage += new PrintPageEventHandler(this.Print);
    pdfServer.PrintQuality = -4;	// High resolution
    int iRetVal = pdfServer.StartPrinting();
    doc.PrinterSettings.PrinterName = pdfServer.NewPrinterName;
    SetupTextReport(m_obPrtMgr);
    doc.Print();
    pdfServer.Wait(1);
    pdfServer.StopPrinting();
    strPdfFile = "/PdrRefports/" + pdfServer.DocumentName;
    pdfServer = null;
    RegisterScriptForReportDisplay(strPdfFile);
}

Open in new window


Setting up grid printing layout

 
private PrintDataTable PrepareDataTable(DataTable dtable)
{
    PrintDataTable dt = new PrintDataTable();
    dt.DrawTableHeader = true;
    dt.DataSource = dtable.DefaultView;

    // Add column information.
    PrintDataColumnCollection cols = dt.Columns;

    PrintDataColumn col = new PrintDataColumn("ProductID", 1.25f, StringAlignment.Far);
    col.HeaderBackColor = Color.Gainsboro;
    col.BackColor = Color.Aquamarine;
    col.HeaderText = "Product ID";
    col.HeaderAlignment = StringAlignment.Center;
    cols.Add(col);

    col = new PrintDataColumn("Name", 1.25f, StringAlignment.Near);
    col.HeaderBackColor = Color.Gainsboro;
    col.ForeColor = Color.Blue;
    col.HeaderText = "Product Name";
    col.HeaderAlignment = StringAlignment.Center;
    cols.Add(col);

    col = new PrintDataColumn("ListPrice", 1.25f, StringAlignment.Far);
    col.HeaderText = "List Price";
    col.HeaderBackColor = Color.Gainsboro;
    col.HeaderAlignment = StringAlignment.Center;
    col.FormatDataColumn += new FormatDataColumnEventHandler(this.FormatDateColumn);
    cols.Add(col);

    return dt;
}

Open in new window


hope this will help you

Regards,

AAK
ASKER CERTIFIED SOLUTION
Avatar of khan_webguru
khan_webguru
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial