6

I have this BoundField in a GridView

<asp:BoundField DataField="ReportId" HeaderText="RId" Visible="false" />

But when I try to get text in that field, it returns empty.

protected void gvwReports_RowCommand(object sender, GridViewCommandEventArgs e)
{
    if (e.CommandName == "ViewSchedule")
    {
        int index = Convert.ToInt32(e.CommandArgument);
        GridViewRow row = gvwReports.Rows[index];
        string s = row.Cells[0].Text;
    }
}

but, it returns a correct value if I change BoundField's .Visible property to true

marc_s
  • 675,133
  • 158
  • 1,253
  • 1,388
Darshana
  • 2,366
  • 6
  • 25
  • 49
  • I'm thinking some optimization is happening behind the scenes and the binding does not actually take place until it becomes visible. – lc. Jul 14 '12 at 18:42
  • can you try using just a simple html style display none – COLD TOLD Jul 14 '12 at 18:47
  • @lc. I just bind data source in normal way. also don't have much experiese of Gridview. – Darshana Jul 14 '12 at 18:51
  • 1
    The `Visible="false"` doesn't just "hide" that field on the client, it doesn't even send the data down **to** the client. So it is impossible to get it back **from** the client. So instead of setting `Visible="false"`, use a `CssClass` that will hide it from view on the client, but the data will still be there. Then the data will be available again server-side on a postback. Just as COLD TOLD suggests, but without the typo :), and remember to set the visibility of the header. – hmqcnoesy Jul 14 '12 at 19:09
  • 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 14 '20 at 04:03

8 Answers8

25

try somethink like this using client side html to hide

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

<asp:BoundField DataField="ReportId" HeaderText="RId" HeaderStyle-CssClass="hidden"   >

</asp:BoundField>
Community
  • 1
  • 1
COLD TOLD
  • 12,989
  • 3
  • 31
  • 49
4

Although it's an year old question (in fact exactly an year old), here's another workaround without using CssClass.

Just after the databind, set visibility of the desired column to false.

gridview1.databind()
gridview1.columns(i).Visibile = False

This will maintain data in viewstate but will not create markup for page.

mishik
  • 9,595
  • 9
  • 39
  • 62
Prashant Gupta
  • 529
  • 5
  • 19
2

the first solution works correctly, but it was necessary add HeaderStyle to hide the header of this column

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

<asp:BoundField DataField="ReportId" HeaderText="RId"  >
    <ItemStyle CssClass="hidden"/>
    <HeaderStyle CssClass="hidden"/>
</asp:BoundField>
Ruben de la Fuente
  • 627
  • 10
  • 23
0

According to my knoweldge when you have made the bound field invisible then you can not access it. Try using TemplateField

Waqar Janjua
  • 6,043
  • 2
  • 23
  • 33
0

I just had the same problem.

Funnily enough a DataGrid won't have that problem, it will allow you to access the data from hidden columns even though it doesn't even render them in the client, because it still adds the information of the hidden columns to the ViewState.

A GridView on the other hand simply ignore the hidden fields even if you set the EnableViewState property to true. The only way is to leave the information there for the client to hide with a style property, like display: none;.

Unfortunate really, I liked the DataGrid behaviour on that, but GridView has other advantages.

Ceottaki
  • 604
  • 1
  • 6
  • 17
0

Seems that if a GridView column is marked as not visible it is not populated at run time so it returns nothing. So, I just populated the Hyperlink from the DataView that is bound to the Gridview remembering to declare the DataView as shared.

I did this in VB asp.net for a GridView that finds searched events from a calendar database.

This worked great for me!

Private Sub GridView1_RowDataBound(sender As Object, e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound

  If (e.Row.RowType = DataControlRowType.DataRow) Then

    Dim ThisHyperLink As HyperLink = e.Row.Cells(0).Controls(0)
    Dim drvRow As DataRowView = dvFoundEvents.Item(e.Row.DataItemIndex)

    EventID = drvRow("EventID")
    ThisHyperLink.NavigateUrl = "<URL>?id=" + EventID

  End If

End Sub
Kermit
  • 1
0

This worked for me:

If the column is a named DataKeyValue on your grid, you can cast the e.Item sent from the row as a DataGridItem and call its DataKeyValue. You'll need to convert it to Int, String, whatever but it will be there even if the column is visible=false.

John Dunagan
  • 1,405
  • 3
  • 18
  • 27
0

At rowDataBound event you can access the field's value using something like:

(((DataRowView)e.Row.DataItem)["your_boundField_dataFieldName"]).ToString();

even if your boundfield visibility is set to false.

Hugo Silva
  • 23
  • 4