Retrieve data from visible false BoundField of Gridview

江枫思渺然 提交于 2019-12-22 05:36:20

问题


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


回答1:


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>



回答2:


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.




回答3:


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>



回答4:


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




回答5:


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.




回答6:


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



回答7:


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.




回答8:


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.



来源:https://stackoverflow.com/questions/11486372/retrieve-data-from-visible-false-boundfield-of-gridview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!