如何在asp.net gridview中获取Item值

时间:2014-03-05 10:51:19

标签: c# asp.net gridview

我有以下挑战。我想从后面的gridview获取项目显示的名称......

将此名称作为参数传递,以获取项目的响应计数。此响应计数将显示在创建的模板字段中的标签控件上。

以下是我的代码示例。

// Create an instance of the LogicLayer
LogicLayer mySurvey = new LogicLayer();
DataLayer getResponseCount = new DataLayer();
protected void Page_Load(object sender, EventArgs e)
{
    gvDepartments.DataSource = mySurvey.SelectDepartment();
    gvDepartments.DataBind();
    gvDepartments.Visible = true;

    // Declare a counter variable
    int dID = 0;
    string responses;
    string deptName;
    //string responseR;
    // Iterate though the gridView to get Dept names and response count values
    foreach (GridViewRow dept in gvDepartments.Rows) 
    {
        //Label respCount = (Label)dept.FindControl("lblResponse").Text
        // the actual way to get your row index
        int rowIndex = dept.RowIndex;
        Label respCount = (Label)gvDepartments.Rows[rowIndex].FindControl("lblResponses");
        responses = respCount.Text.ToString();
        deptName = dept.Cells[rowIndex].ToString(); // Get the department name on the gridview
        // get the responseCount for each of the departments and Map to Labels
        responses = getResponseCount.ResponseCount(deptName);
        dID++;
    }
}

这是我网格的图片视图。

enter image description here

3 个答案:

答案 0 :(得分:2)

试试这个

GridViewRow row = gvDepartments.Rows[rowIndex];

String value = row.Cells[2].Text.ToString(); // I assume 2 is the column index of response you could use `FindControl` method for template fields

答案 1 :(得分:1)

经过大量搜索以获得上述问题的解决方案。我使用了RowDataBound事件,如下所示

// Create an instance of the LogicLayer
LogicLayer mySurvey = new LogicLayer();
DataLayer getResponseCount = new DataLayer();
protected void Page_Load(object sender, EventArgs e)
{
    gvDepartments.DataSource = mySurvey.SelectDepartment();
    gvDepartments.DataBind();
    gvDepartments.Visible = true;
}
protected void gvDepartments_RowDataBound(object sender, GridViewRowEventArgs e)
{
    // Declare a counter variable
    int dID = 0;
    string responses;
    string deptName;
    //string responseR;

    // Iterate though the gridView to get Dept names and response count values

    foreach (GridViewRow dept in gvDepartments.Rows)
    {
        //Label respCount = (Label)dept.FindControl("lblResponse").Text
        // the actual way to get your row index
        int rowIndex = dept.RowIndex;
        Label respCount = (Label)gvDepartments.Rows[dID].FindControl("lblResponses");
        responses = respCount.Text.ToString();
        // dept.Cells[rowIndex].ToString();  Get the department name on the gridview
        **deptName = e.Row.Cells[1].Text.ToString();**

        // get the responseCount for each of the departments and Map to Labels
        responses = getResponseCount.ResponseCount(deptName);

        dID++;
    }
}

答案 2 :(得分:0)

找到deptName时出错。您必须使用Cell Index代替rowIndexrowIndex可能会根据行数增长。由于单元格或0基于索引,因此您可以在索引“1”处找到deptName,除非更改列位置,否则它将保持不变。 您可以获得deptName的值,如下所示

deptName = dept.Cells[1].ToString();