Search code examples
asp.netgridviewboundfield

How to use a hidden Boundfield in a GridView in ASP.NET?


I am binding a GridView using a DataSource in asp.net, and i would like to have some hidden BoundFields so I can access those values in my RowDataBound Function. However, when I set Visible="False" on these BoundFields the values do not get set and are always blank in the RowDataBound function.

Is there a way to do this? I've seen some suggestions of setting the style on the BoundField to hidden but this did not work for me either. Ideally I don't even want a column created in the gridview, I just want these values to be hidden so I can access them. Thanks!


Solution

  • Have you tried using the DataItem from the GridViewRowEventArgs passed to your RowDataBound method?

    Like:

    protected void MyGridView_OnRowDataBound(object sender, GridViewRowEventArgs e)
    {
        if(e.Row.RowType == DataControlRowType.DataRow)
        {
            DataRowView rowView = (DataRowView)e.Row.DataItem;
            // do something with rowView["myHiddenField"]
        }
    }
    

    Check MSDN here.