You must Sign In to post a response.
  • Category: .NET

    How to split on data grid view rows in same page using print time

    I am created windows application using c# 2010, in my application I am using data grid view.
    I am entered above 50 rows in output. I want print all rows for my data grid view records.
    But I want sing page print all records don't want second page, in first columns print only 20 rows and same columns print in same page continuous 21 to 40 and third columns balance 10 print.
    Ex:
    Slno mts cm Slno mts cm Slno mts cm
    1 21 41
    2 22 42
    .. .. ..
    20 40 50
    How to split on data grid view rows in same page. Any one guide me.
  • #767121
    There are couple of ways to print gridview columns
    1. Selected row (Page 1 rows) can be taken in datatable and then print it
    2. Take a gridview as bitmap and print it using below code snippet

    private void printDocument1_PrintPage(object sender, System.Drawing.Printing.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);
    }
    OR

    private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
    {
    System.Drawing.Font fntString = new Font("Times New Roman", 10, FontStyle.Bold);
    //first row values
    e.Graphics.DrawString("Id : ", fntString, Brushes.Black, 24, 70);
    e.Graphics.DrawString(dataGridView1.Rows[0].Cells[0].Value.ToString(), fntString, Brushes.Black, 100, 70);
    e.Graphics.DrawString("Name : ", fntString, Brushes.Black, 24, 90);
    e.Graphics.DrawString(dataGridView1.Rows[0].Cells[1].Value.ToString(), fntString, Brushes.Black, 100, 90);
    //second row values
    e.Graphics.DrawString("Id : ", fntString, Brushes.Black, 180, 70);
    e.Graphics.DrawString(dataGridView1.Rows[0].Cells[0].Value.ToString(), fntString, Brushes.Black, 220, 70);
    e.Graphics.DrawString("Name: ", fntString, Brushes.Black, 180, 90);
    e.Graphics.DrawString(dataGridView1.Rows[0].Cells[1].Value.ToString(), fntString, Brushes.Black, 220, 90);
    }

    Thanks
    Koolprasd2003
    Editor, DotNetSpider MVM
    Microsoft MVP 2014 [ASP.NET/IIS]

  • #767122
    I am using your second type code, what i am expect, here i am attached sample image format, once check and guide me.

    Delete Attachment

  • #767126
    Sir your second method code working good thanks a lot. But one small problem, I am not compulsory enter all cell values, I enter only alternative.
    Ex :
    Sno mts cm
    1 5
    2 3
    3 2
    4 6 6
    This way I am enter the cells, but click a print button error is came
    Error: Object reference not set on instance of an object.
    How to solve above error give me some ideas

  • #767136
    Hi,

    Object reference not set to an instance of an object, means you are trying to assign null value to some object that time you got this type of error message, put a break point and check in which line you got this error and check the value of that line whether there is a value in that or not

    --------------------------------------------------------------------------------
    Give respect to your work, Instead of trying to impress your boss.

    N@veen
    Blog : http://naveens-dotnet.blogspot.in/


Sign In to post your comments