How to get values from template fields in GridView?

前端 未结 5 736
伪装坚强ぢ
伪装坚强ぢ 2021-01-05 06:56

This is my markup of GridView.


    
        
            

        
5条回答
  •  Happy的楠姐
    2021-01-05 07:11

    Your are missing a type cast. Do it like this-

    Label name = (Label)GridView2.Rows[i].Cells[j].FindControl("lblname");
    xlWorkSheet.Cells[i + 1, j + 1] = name.Text;
    

    Update- If you can name your labels as Label0 and Label1, then in the second for loop-

    for (int j = 0; j < GridView2.Rows[i].Cells.Count; j++)
      {
         Label xyz = (Label)GridView2.Rows[i].Cells[j].FindControl("Label"+j);
         xlWorkSheet.Cells[i + 1, j + 1] = xyz.Text;
      }
    

    For Header text- string hText = GridView2.HeaderRow.Cells[your column number].Text;

提交回复
热议问题