سؤال

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

هل كانت مفيدة؟

المحلول

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>

نصائح أخرى

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.

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>

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

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.

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

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.

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.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top