Retrieve data from visible false BoundField of Gridview
Asked Answered
R

8

7

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

Radiotelephony answered 14/7, 2012 at 18:39 Comment(6)
I'm thinking some optimization is happening behind the scenes and the binding does not actually take place until it becomes visible.Hausa
can you try using just a simple html style display noneChitwood
@Hausa I just bind data source in normal way. also don't have much experiese of Gridview.Radiotelephony
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.Coagulant
Does this answer your question? How to hide a column (GridView) but still access its value?Pindling
codingfusion.com/Post/…Gelt
C
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>
Chitwood answered 14/7, 2012 at 18:52 Comment(5)
+1 I didn't read your answer before posting my own. :) I'll remove mine.Coagulant
@Coagulant good, I agreed with you point that when we set visible ='true' the entire row is not sent to the client.Intermixture
Usually the header also should be hidden, so we need to add HeaderStyle-CssClass="hidden" to BoundField tag.Speleology
Add this also <HeaderStyle CssClass="hidden"/>Juryrig
This only hide header only\Tahsildar
M
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.

Mantissa answered 14/7, 2013 at 6:54 Comment(0)
O
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>
Oswin answered 3/2, 2015 at 11:50 Comment(0)
I
0

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

Intermixture answered 14/7, 2012 at 18:58 Comment(0)
M
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.

Mort answered 20/9, 2012 at 11:24 Comment(0)
S
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
Sepulcher answered 14/2, 2014 at 16:6 Comment(0)
P
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.

Peristalsis answered 15/5, 2014 at 15:55 Comment(0)
I
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.

Ipa answered 15/7, 2014 at 13:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.