locked
printing Datagridview with selected column names RRS feed

  • Question

  • Hi,

    I found couple reference about printing datagridview with selected colums on Google.  However, I want to print a certain columns that are selected by name.  How do I do that.  Specific code to help me in this case is appreciated!

    Thanks,

    tt.

    • Changed type Bob Wu-MT Tuesday, September 18, 2012 7:14 AM
    Thursday, September 13, 2012 6:25 PM

Answers

  • I'm glad to hear that.

    Good day.


    Bob Wu [MSFT]
    MSDN Community Support | Feedback to us

    • Marked as answer by tt-2010 Tuesday, September 18, 2012 12:21 PM
    Tuesday, September 18, 2012 7:14 AM
  • Hi tt-2010,

    I'm not sure what do you mean by print a certain columns that are selected by name. .

    However, I think the following document is a good example to print part of datagridview.

    http://www.codeproject.com/Articles/16670/DataGridView-Printing-by-Selecting-Columns-and-Row

    Anyway,you can add condition after

    if (!PrintDGV.SelectedColumns.Contains(GridCol.HeaderText)) continue;

    //or if (!GridCol.Visible) continue;

    // you can search GridCol.Visible or GridCol.HeaderText to find the statements

     in private static void PrintDoc_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e) event handler and private static void PrintDoc_BeginPrint(object sender, System.Drawing.Printing.PrintEventArgs e) event handler to skip count and print some columns.

    Best Regards,


    Bob Wu [MSFT]
    MSDN Community Support | Feedback to us


    • Edited by Bob Wu-MT Friday, September 14, 2012 9:38 AM
    • Marked as answer by Bob Wu-MT Wednesday, September 19, 2012 3:18 AM
    Friday, September 14, 2012 9:38 AM

All replies

  • Hi tt-2010,

    I'm not sure what do you mean by print a certain columns that are selected by name. .

    However, I think the following document is a good example to print part of datagridview.

    http://www.codeproject.com/Articles/16670/DataGridView-Printing-by-Selecting-Columns-and-Row

    Anyway,you can add condition after

    if (!PrintDGV.SelectedColumns.Contains(GridCol.HeaderText)) continue;

    //or if (!GridCol.Visible) continue;

    // you can search GridCol.Visible or GridCol.HeaderText to find the statements

     in private static void PrintDoc_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e) event handler and private static void PrintDoc_BeginPrint(object sender, System.Drawing.Printing.PrintEventArgs e) event handler to skip count and print some columns.

    Best Regards,


    Bob Wu [MSFT]
    MSDN Community Support | Feedback to us


    • Edited by Bob Wu-MT Friday, September 14, 2012 9:38 AM
    • Marked as answer by Bob Wu-MT Wednesday, September 19, 2012 3:18 AM
    Friday, September 14, 2012 9:38 AM
  • thanks for your help and I am sorry to confuse you.  What I mean is if I have a datagridview with 5 columns, their names are A,B,C,D,E.  I only want to print data of column name as A, C and E.

    I have used the same reference link that you show me and I have figured out the way to handle what I want.  However, I have problem with the part of code at the end of the while loop.

                 //while loop start here

                 {

                   ........

                  }

                  RowPos++;
                // For the first page it calculates Rows per Page
                if (PageNo == 1) RowsPerPage++;
            }

            if (RowsPerPage == 0) return;

            // Write Footer (Page Number)
            DrawFooter(e, RowsPerPage);

    why DrawFooter(e, RowsPerPage) not DrawFooter(e, PageNo).  I don't understand much about the part of DrawFooter code.  Could you explain little bit more for me so that I can print out my page number at the end of the page.  If I have some code to print for pageNo, where should I put these code   in the program above       string page = "Page " + pageNo.ToString();    e.Graphics.DrawString(page, font, Brushes.Black, 400, 1010);                

    Also, when the number of row is of datagridview is big.  There are a lot more rows need to print, it supposed to be print on the next page and the iRow index should be continue from end of page 1, but when I run program in debug mode, the iRow is return to index 0 instead of continue and it will repeat printing of the same data of the first page.  I might be wrong on this.  Anyway, if number data row is not too big and fit on 1 page.  I got it work.  But, if it is large number of data row and should print out with more than one page, I still have not got it right.

    Thanks,

    • Edited by tt-2010 Friday, September 14, 2012 9:43 PM
    Friday, September 14, 2012 8:05 PM
  • thanks for your help and I am sorry to confuse you.  What I mean is if I have a datagridview with 5 columns, their names are A,B,C,D,E.  I only want to print data of column name as A, C and E.

    So, you need to Skip other columns(which are not A,C and E) when it calculating total widths,when save width & height of headers and column type, when it draw columns and when it draw columns contents.

    I edited the sample and add the following code to filter the columns,so it will not draw other columns(which are not A,C and E).

    //3 places
    //Skip if the current column is not ID, FirstName or Image
    if (!GridCol.Name.Equals("ID") && !GridCol.Name.Equals("FirstName") && !GridCol.Name.Equals("Image")) continue;
    
    //1 place, when draw columns contents
    //Skip if the current column is not ID, FirstName or Image
    if (!Cel.OwningColumn.Name.Equals("ID") && !Cel.OwningColumn.Name.Equals("FirstName") && !Cel.OwningColumn.Name.Equals("Image")) continue;

    why DrawFooter(e, RowsPerPage) not DrawFooter(e, PageNo).  I don't understand much about the part of DrawFooter code.  Could you explain little bit more for me so that I can print out my page number at the end of the page.  If I have some code to print for pageNo, where should I put these code   in the program above       string page = "Page " + pageNo.ToString();    e.Graphics.DrawString(page, font, Brushes.Black, 400, 1010);                

    If you read the logic of DrawFooter method, it use RowsPerPage to get the total number of the page.

    string PageNum = PageNo.ToString() + " of " + 
                    Math.Ceiling((double)(cnt / RowsPerPage)).ToString();

    The PageNo value is current page number and since it is a class level, it used in the method directly. Actually, we can use RowsPerPage directly in the method too.

    Also, when the number of row is of datagridview is big.  There are a lot more rows need to print, it supposed to be print on the next page and the iRow index should be continue from end of page 1, but when I run program in debug mode, the iRow is return to index 0 instead of continue and it will repeat printing of the same data of the first page.  I might be wrong on this.  Anyway, if number data row is not too big and fit on 1 page.  I got it work.  But, if it is large number of data row and should print out with more than one page, I still have not got it right.

    Did  you use the code project sample? The sample will print two pages and both page are print correctly.

    Can you show us your current code or share a demo to reproduce this issue?

    Best Regards,


    Bob Wu [MSFT]
    MSDN Community Support | Feedback to us


    • Edited by Bob Wu-MT Monday, September 17, 2012 8:25 AM
    Monday, September 17, 2012 8:22 AM
  • Appreciate! for your helping.  I try to correct my code and also follow your recommendation.  I am able to make my printing multiple pages to work fine now. 

    Thanks,

    tt.


    • Edited by tt-2010 Monday, September 17, 2012 9:53 PM
    Monday, September 17, 2012 8:41 PM
  • I'm glad to hear that.

    Good day.


    Bob Wu [MSFT]
    MSDN Community Support | Feedback to us

    • Marked as answer by tt-2010 Tuesday, September 18, 2012 12:21 PM
    Tuesday, September 18, 2012 7:14 AM