-1

I have a gridview that for a part of the gridview, I want to do something and for another part, I want to do another thing. Like from the index 0 to index 14, do this. From the index 15 to 30, do that.

My idea is I'm generating a pdf file, and if the GridView has 0-14 rows, it creates 1 page. If it has 15-30 rows, it creates another page with a different design and so on... This happens in a Click event of a "Print" button.

Is it possible and how?

Here's some code I have for this:

string listeMarchandises = String.Empty;                    
var i = 0;

foreach (GridViewRow row in GridViewMarchandises.Rows)
{
    if (i <= 14)
    {
        if (row.RowType == DataControlRowType.DataRow)
        {
            Label LabelNumCpte = (Label)row.FindControl("LabelNumCpte");
            Label LabelNumCc = (Label)row.FindControl("LabelNumCc");
            Label LabelNbrCom = (Label)row.FindControl("LabelNbrCom");
            TextBox TextBoxNbrRec = (TextBox)row.FindControl("TextBoxNbrRec");
            Label LabelDescription = (Label)row.FindControl("LabelDescription");
            TextBox TextBoxCoutUnitaire = (TextBox)row.FindControl("TextBoxCoutUnitaire");
            Label LabelCoutTotal = (Label)row.FindControl("LabelCoutTotal");
            listeMarchandises += "<tr valign=\"top\"><td align=\"center\">" + LabelNumCpte.Text.Trim() + "</td><td align=\"center\">" + LabelNumCc.Text.Trim() + "</td><td align=\"center\">" + LabelNbrCom.Text.Trim() + "</td><td align=\"center\">" + TextBoxNbrRec.Text.Trim() + "</td><td>&nbsp;" + LabelDescription.Text.Trim() + "</td><td align=\"right\">" + TextBoxCoutUnitaire.Text.Trim() + " $&nbsp;" + "</td><td align=\"right\">" + LabelCoutTotal.Text.Trim() + " $&nbsp;" + "</td></tr>";
        }
        i++;
    }

    if (i >= 15 && i <= 30)
    {
        //do the other thing
    }
}

Thanks in advance!

Yannick
  • 17
  • 8
  • You must read this SO post and think about your scenario whether you want first n index items or first n rows in collection or something else based on some condition. http://stackoverflow.com/questions/43021/how-do-you-get-the-index-of-the-current-iteration-of-a-foreach-loop – haraman Nov 02 '15 at 19:47
  • why don't you state specifically what it is that you want perhaps you could do you code in the OnDataBound event. can you be more specific – MethodMan Nov 02 '15 at 19:48
  • I'm very sorry for my explanation being not clear. My idea is I'm generating a pdf file, and if the GV has 0-14 rows, it creates 1 page. If it has 15-30 rows, it creates another page and so on... @JohnPaul helped me with his code and problem solved. :) – Yannick Nov 03 '15 at 19:53

1 Answers1

0

Use the row data bound event:

    protected void gdv_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        var index = e.Row.RowIndex;
        if(index > 14)
        {
             //Do stuff
        }
        else if (index >= 15 && index <= 30)
        {
             //Do other stuff
        }
    }
John Paul
  • 787
  • 2
  • 5
  • 16
  • u miss the datarow type check. if (e.Row.RowType == DataControlRowType.DataRow) { ... your logic... } – g2000 Nov 02 '15 at 21:14
  • TY it works! Also TY for your understanding. My idea is I'm generating a pdf file, and if the GV has 0-14 rows, it creates 1 page. If it has 15-30 rows, it creates another page and so on... So the event was not in the RowDataBound. I actually make it happen in a Click event of a "Print" button – Yannick Nov 03 '15 at 19:40