解决无法获取 GridView 隐藏列值问题

今天遇到了一个要获取GridView隐藏列值的问题,试了好几种方法,要么获取不到,要么获取到了类列的值也隐藏了,但在样式中这一列会多出一块,,但最后找到了一个功能实现而且实现了列完美隐藏的方法和大家分享一下:

aspx页面:

加上样式:

<style type="text/css">
.hidden
{
display: none;
}
</style>

需要隐藏列代码:(用了上面的hidden样式)

1 <asp:BoundField DataField="compl_objtypename">
2 <HeaderStyle CssClass="hidden" />
3 <ItemStyle Width="0px" />
4 </asp:BoundField>

后台代码:在GirdViewRowDataBound事件中,把隐藏列的值在第五列为孔的时候赋值给第五列

  protected void gvComplainInfo_RowDataBound(object sender, GridViewRowEventArgs e)
    {  
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                if ((string.IsNullOrEmpty(e.Row.Cells[5].Text) || e.Row.Cells[5].Text == "&nbsp;"))
                {
                    e.Row.Cells[5].Text = e.Row.Cells[11].Text;//e.Row.Cells[11] 隐藏列
                }
                e.Row.Cells[11].Visible = false;//设置隐藏类绑定数据行不可见
            }    
    }

效果(隐藏列在最后一列):

 

原文地址:https://www.cnblogs.com/guohuiru/p/3324555.html