Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/37.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从一个索引到另一个索引的GridView foreach_C#_Asp.net_Gridview_Indexing_Foreach - Fatal编程技术网

C# 从一个索引到另一个索引的GridView foreach

C# 从一个索引到另一个索引的GridView foreach,c#,asp.net,gridview,indexing,foreach,C#,Asp.net,Gridview,Indexing,Foreach,我有一个gridview,对于gridview的一部分,我想做一些事情,对于另一部分,我想做另一件事情。 就像从索引0到索引14一样,这样做。 从指数15到30,这样做 我的想法是生成一个pdf文件,如果GridView有0-14行,它将创建一个页面。如果它有15-30行,它会创建另一个具有不同设计的页面,以此类推。。。 这发生在“打印”按钮的单击事件中 有可能吗?怎么可能 以下是我的一些代码: string listeMarchandises = String.Empty;

我有一个gridview,对于gridview的一部分,我想做一些事情,对于另一部分,我想做另一件事情。 就像从索引0到索引14一样,这样做。 从指数15到30,这样做

我的想法是生成一个pdf文件,如果GridView有0-14行,它将创建一个页面。如果它有15-30行,它会创建另一个具有不同设计的页面,以此类推。。。 这发生在“打印”按钮的单击事件中

有可能吗?怎么可能

以下是我的一些代码:

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
    }
}
string listmarchandises=string.Empty;
var i=0;
foreach(GridViewMarchandises.Rows中的GridViewRow行)
{

如果(i=15&&i使用行数据绑定事件:

    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
        }
    }
受保护的void gdv_RowDataBound(对象发送方,GridViewRowEventArgs e)
{
var指数=e.Row.RowIndex;
如果(索引>14)
{
//做事
}

否则如果(index>=15&&index您必须阅读这篇文章,并考虑您的场景,是希望集合中的前n个索引项还是前n行,还是基于某些条件的其他内容。为什么不具体说明您想要的内容?也许您可以在OnDataBound事件中编写代码。您能更具体一点吗解释不清楚。我的想法是我正在生成一个pdf文件,如果GV有0-14行,它将创建一个页面。如果GV有15-30行,它将创建另一个页面,依此类推…@JohnPaul帮助我完成了代码并解决了问题。:)你错过了数据行类型检查。如果(e.Row.RowType==DataControlRowType.datarow){…你的逻辑…}TY有效!也是TY供您理解。我的想法是生成一个pdf文件,如果GV有0-14行,它将创建一个页面。如果GV有15-30行,它将创建另一个页面,依此类推…因此该事件不在RowDataBound中。我实际上是在“打印”按钮的单击事件中实现的