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

17.1k views Asked by At

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!

3

There are 3 answers

1
Forgotten Semicolon On BEST ANSWER

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.

0
Geeth On

For this i will suggest you to use DataKey.

Pls refer this link http://www.codeproject.com/KB/grid/Data_presentation.aspx

eg:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource1"

DataKeyNames="emp_id,Code">

int EmployeeID = Convert.ToInt32(GridView1.DataKeys[row.RowIndex].Values[0]);
  int DepartementID = Convert.ToInt32(GridView1.DataKeys[row.RowIndex].Values[1]);

in DataRow bound:

((GridView)sender).DataKeys[e.Row.RowIndex].Value.ToString();

Geetha

2
RandomWebGuy On

Odd, How are you doing it? I know I've done this before using:

<asp:HiddenField ID="HiddenField1" runat="server" Value='<%# Eval("ColumnName")%>'/>