Darshana
Darshana

Reputation: 2548

Retrieve data from visible false BoundField of Gridview

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

Upvotes: 7

Views: 39758

Answers (8)

COLD TOLD
COLD TOLD

Reputation: 13599

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>

Upvotes: 25

Ruben de la Fuente
Ruben de la Fuente

Reputation: 695

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>

Upvotes: 2

Hugo Silva
Hugo Silva

Reputation: 23

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.

Upvotes: 0

John Dunagan
John Dunagan

Reputation: 1465

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.

Upvotes: 0

Kermit
Kermit

Reputation: 1

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

Upvotes: 0

Prashant Agarwal
Prashant Agarwal

Reputation: 809

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.

Upvotes: 4

Ceottaki
Ceottaki

Reputation: 744

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.

Upvotes: 0

Waqar Janjua
Waqar Janjua

Reputation: 6123

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

Upvotes: 0

Related Questions