对于每个循环不读取gridview中的第一行

时间:2014-03-12 09:03:10

标签: c# asp.net gridview foreach

我有一个foreach循环没有读取网格视图中的第一行。当我进行调试时,它从GridView的第二行开始给我部门名称和responseCount。

基本上,从gridview行获取的部门名称将作为参数传递到函数getDepartmentResponseCount.ResponseCount(deptName),该函数返回该部门的计数。

foreach循环不是从gridview中的第一行读取的。以下是源代码。

protected void gvDepartments_RowDataBound(object sender, GridViewRowEventArgs e)
{
    // Empty the string variable
    myDeptResponseCount = "";
    // Iterate though the gridView to get Dept names and response count values

    foreach (GridViewRow dept in gvDepartments.Rows)
    {
        // the actual way to get your row index            
        int rowIndex = dept.RowIndex;

        //Label respCount = dept.FindControl("lblResponses"+ dID) as Label;
        Label lblResponses = (Label)gvDepartments.Rows[rowIndex].FindControl("lblResponses" + dID);

        deptName = e.Row.Cells[1].Text.ToString();            

        // get the responseCount for each of the departments
        myDeptResponseCount = getDepartmentResponseCount.ResponseCount(deptName);

        //lblResponses.Text = myResponseCount;
        deptCount.Add(deptName);
    }

    //dID++;
    deptNameCount.Add(deptName);        
}

enter image description here

1 个答案:

答案 0 :(得分:2)

请试试这个

protected void gvDepartments_RowDataBound(object sender, GridViewRowEventArgs e)
{
    // Empty the string variable
    myDeptResponseCount = "";
    // Iterate though the gridView to get Dept names and response count values

    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        // the actual way to get your row index            
        int rowIndex = e.Row.RowIndex;

        //Label respCount = dept.FindControl("lblResponses"+ dID) as Label;
        Label lblResponses = (Label)e.Row.FindControl("lblResponses" + dID);

        deptName = e.Row.Cells[1].Text.ToString();

        // get the responseCount for each of the departments
        myDeptResponseCount = getDepartmentResponseCount.ResponseCount(deptName);

        //lblResponses.Text = myResponseCount;
        deptCount.Add(deptName);
    }

    //dID++;
    deptNameCount.Add(deptName);
}