3

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!

demongolem
  • 9,474
  • 36
  • 90
  • 105
Drew
  • 2,601
  • 6
  • 42
  • 65
  • 1
    Work directly with `e.Row.DataItem`. A second option would be to use invisible TemplateFields. – Tim Schmelter Feb 11 '11 at 22:07
  • 1
    I assume you've tried this with everything the same except `Visible="True"`, right? I'm not sure why the BoundField wouldn't still be accessible. – Justin Morgan - On strike Feb 11 '11 at 22:12
  • sorry it took me so long to respond guys, wasnt able to check over the weekend. – Drew Feb 14 '11 at 13:23
  • Does this answer your question? [How to hide a column (GridView) but still access its value?](https://stackoverflow.com/questions/5376278/how-to-hide-a-column-gridview-but-still-access-its-value) – Michael Freidgeim Dec 13 '20 at 23:41

3 Answers3

2

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

<asp:HiddenField ID="HiddenField1" runat="server" Value='<%# Eval("ColumnName")%>'/>
RandomWebGuy
  • 1,439
  • 11
  • 23
2

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

Geeth
  • 5,282
  • 21
  • 82
  • 133
1

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.

Forgotten Semicolon
  • 13,909
  • 2
  • 51
  • 61
  • i was previously using if (e.Row.RowType == DataControlRowType.DataRow) { string Name = e.Row.Cells[5].Text; } which may have been why it didnt work – Drew Feb 14 '11 at 13:25