How to get values from template fields in GridView?

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

This is my markup of GridView.


    
        
            

        
5条回答
  •  刺人心
    刺人心 (楼主)
    2021-01-05 07:02

    Try using this code, I had a similar problem. I think this code is more dynamic and you do not have to find the name of the label every time. But to keep the correspondence between the controls And the index Controls [ ]:

    for (int row = 1; row <= totalRows; row++)
    {
        for (int col = 0; col < totalCols; col++)
        {
            if (GridView1.Columns[col].Visible)
            {
                if (String.IsNullOrEmpty(GridView1.Rows[row - 1].Cells[col].Text))
                {
                    if (GridView1.Rows[row - 1].Cells[col].Controls[1].GetType().ToString().Contains("Label"))
                    {
                        Label LB = (Label)GridView1.Rows[row - 1].Cells[col].Controls[1];
                        workSheet.Cells[row + 1, col + 1].Value = LB.Text;
                    }
                    else if (GridView1.Rows[row - 1].Cells[col].Controls[1].GetType().ToString().Contains("LinkButton"))
                    {
                        LinkButton LB = (LinkButton)GridView1.Rows[row - 1].Cells[col].Controls[1];
                        workSheet.Cells[row + 1, col + 1].Value = LB.Text;
                    }
                }
                else
                {
                    workSheet.Cells[row + 1, col + 1].Value = GridView1.Rows[row - 1].Cells[col].Text;
                }
    

提交回复
热议问题