2014-02-12 21 views
0

我想選擇的GridView選擇的GridView的當前行不工作

的當前行
int i = 0; 
    int a1, a2,a3; 
    string ida = Session["id"].ToString(); 
    foreach (GridViewRow gv in grdreport.Rows) 
    { 
    Label id = (Label)grdreport.Rows[i].Cells[6].FindControl("lblid"); 
    Label lblqus = (Label)grdreport.Rows[i].Cells[3].FindControl("Label1"); ; 
    Label lblans = (Label)grdreport.Rows[i].Cells[4].FindControl("Label2"); 
    try //geting Officer ID 
    { 
     int j = 0; 
     DataTable dtofc = new DataTable(); 
     conn.Open(); 
     SqlCommand cmd = new SqlCommand("SP_FAQ_CCA", conn); 
     cmd.CommandType = CommandType.StoredProcedure; 
     cmd.Parameters.AddWithValue("qryflg", "Officer1"); 
     SqlDataAdapter da = new SqlDataAdapter(cmd); 
     da.Fill(dtofc); 
     officerDept = dtofc.Rows[j]["DeptId"].ToString(); 
     QAId = dtofc.Rows[j]["QAId"].ToString(); 
     conn.Close(); 
    } 

只選擇GridView控件的第一行,即使我點擊了第二排。 我也試過這樣:

Label id = (Label)grdreport.currentrow.cell[6].FindControl("lblid"); 
Label id = (Label)gv.FindControl("lblid"); 

,但它不工作爲好,請幫我這。

+0

代碼rowcommand事件處理中? –

+0

如何選擇行(通過單擊複選框或按鈕?) – Nimmi

+0

不僅發佈內部代碼的完整功能。 –

回答

1

它應該是:

Label id = (Label)gv.Cells[6].FindControl("lblid"); 
    Label lblqus = (Label)gv.Cells[3].FindControl("Label1"); 
    Label lblans = (Label)gv.Cells[4].FindControl("Label2"); 

如果你想成爲一個按鈕單擊事件處理程序中:

protected void btn_Click(object sender, EventArgs e) 
    { 
     //clicked button 
     LinkButton btn= (LinkButton)sender; 

     // row of the clicked button 
     GridViewRow containerRow = (GridViewRow)(btn).NamingContainer; 

    Label id = (Label)containerRow .Cells[6].FindControl("lblid"); 
    Label lblqus = (Label)containerRow .Cells[3].FindControl("Label1"); 
    Label lblans = (Label)containerRow .Cells[4].FindControl("Label2"); 

/// remaining code 

} 
+0

先生其在linkbuttons點擊事件 –

+0

檢查更新的答案。 –

+0

thnaku非常先生你的幫助.. –