3

I have the following challenge. I want to get the name of the Items display on the gridview from the side behind...

Pass this name as a parameter to get the count of responses for the item. This response count will be displayed on a label controls in the template fields created.

Below is my code sample.

// 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++;
    }
}

This is the pictural view of my grid.

enter image description here

2
  • possibly better calculate respCount from mySurvey.SelectDepartment() instead of from grid Commented Mar 5, 2014 at 10:58
  • You certainly don't understand my question. The method mySurvey.SelectDepartment() returns a DataSet which is bounded to the GridView. I need the names of the Bounded Departments to get the count of responses for that department. Commented Mar 5, 2014 at 11:34

3 Answers 3

2

Try this

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
Sign up to request clarification or add additional context in comments.

Comments

1

After much search to get solution to my question above. I used the RowDataBound event as seen below

// 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++;
    }
}

Comments

0

There is a fault in finding the deptName. You have to use Cell Index instead of rowIndex. rowIndex may grow based on the number of rows. Since the cells or 0 based indexed, so you can find the deptName at index "1", which will be constant unless the column position is changed. You can get the value of deptName as following

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.