1

So i have some data stored into an object, and I need to retrieve the data and output it in a table like format. I have tried using format specifiers and due to the width of characters the information changes. I need to create something similar to a table, return that object, and then be able to print the information on a piece of paper without printing cell lines. I have tried a data table but I can't see the info in the table. what is the easiest way to do this? It is a windows form application. The user is saving the information in 3 classes. using a list. I have tried using spaces and \t to tab the format. But depending on the width of the character the formatting is off by a few spaces. Also the length of the data can go back and forth between 1-3 characters.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
  • 3
    ASP? WinForms? WPF? We need more information. – Dave Zych Feb 04 '13 at 18:25
  • Need more details. With what you have given me all I can tell you is "Generate a custom object storage method and a custom printout method that you can format how you want it" We do not know the structure of the object or the data, so we can't get any more specific with the answers...or at least, I can't. – Nevyn Feb 04 '13 at 18:31
  • 1
    depending on how much time / effort you can invest on this and on how nice the output should be, you can either create an html file (easily, as it is simply a matter of rendering your content with a string.ToFormat and add the table related tags) or use a report engine. for professional level reports, DevExpress XtraReports is very powerful, or have a look here: http://stackoverflow.com/questions/788923/reporting-free-open-source-alternatives-to-crystal-reports-in-winforms – Davide Piras Feb 04 '13 at 18:32

1 Answers1

1

Just populate a datatable from the object and set the datagridview source to the datatable.

public class Form1
{
    public void BuildDataTable()
    {
        DataTable dt = new DataTable("Customers");  
        dt.Columns.Add(new DataColumn("LastName")); 
        dt.Columns.Add(new DataColumn("FirstName")); 

        // Populate the table 
        dt.Rows.Add("Baggins", "Bilbo");
    }


    private void Button2_Click(System.Object sender, System.EventArgs e)
    {
        PrintDocument1.Print();
    }

    private void PrintDocument1_PrintPage(object sender, PrintPageEventArgs e)
    {
         Bitmap bm = new Bitmap(this.DataGridView1.Width, this.DataGridView1.Height);
         DataGridView1.DrawToBitmap(bm, new Rectangle(0, 0, this.DataGridView1.Width, this.DataGridView1.Height));
         e.Graphics.DrawImage(bm, 0, 0);
    }
}
Sergey Berezovskiy
  • 232,247
  • 41
  • 429
  • 459
will simmons
  • 189
  • 2