2014-03-05 81 views
3

我有以下挑戰。我想從後面獲取gridview上的Items顯示名稱...如何在asp.net中獲取項目值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

+0

可能更好地'mySurvey.SelectDepartment計算'respCount'()',而不是從電網 – Grundy

+0

你肯定不明白我的問題。方法mySurvey.SelectDepartment()返回一個綁定到GridView的DataSet。我需要有界部門的名字才能得到該部門的答覆。 – Guzzyman

回答

0

有在尋找deptName故障。您必須使用Cell Index而不是rowIndexrowIndex可能會根據行數增長。由於單元格或索引爲0,因此您可以在索引「1」處找到deptName,除非列位置發生更改,否則該索引將保持不變。 你可以得到的deptName值如下

deptName = dept.Cells[1].ToString(); 
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

很多搜​​索之後得到解決我的問題上面。我用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++; 
    } 
} 
相關問題