我想选择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 :(得分: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
}