Extract data from visible false BoundField from Gridview

I have a BoundField in a GridView

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

But when I try to get the text in this 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 returns the correct value if I change the BoundField's .Visible to true

+6
source share
8 answers

try something like this using client-side html to hide

 <style type="text/css"> .hidden { display:none; } </style> <asp:BoundField DataField="ReportId" HeaderText="RId" > <ItemStyle CssClass="hidden"/> </asp:BoundField> 
+23
source

Although this is a question for a year (actually exactly a year), here is another workaround without using CssClass.

Immediately after data binding, set the visibility of the desired column to false.

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

This will save the data in the viewstate, but will not create markup for the page.

+4
source

the first solution works correctly, but it was necessary to add a 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> 
+2
source

According to my knoweldge, when you made an associated field invisible, you cannot access it. Try using TemplateField

0
source

I had the same problem.

Oddly enough, the DataGrid will not have this problem, it will allow you to access data from hidden columns, even if it does not even display them on the client, as it still adds information about hidden columns to the ViewState.

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

Unfortunately, I liked the behavior of the DataGrid, but the GridView has other advantages.

0
source

It appears that if the GridView column is marked as invisible, it does not fill at run time, so it does not return anything. So I just populated the hyperlink from the DataView that is bound to the Gridview, forgetting to declare the DataView as shared.

I did this in asp.net VB for GridView, which finds the events found from the 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 
0
source

This worked for me:

If the column is the name DataKeyValue in your grid, you can pass the e.Item sent from the row as a DataGridItem and call it DataKeyValue. You will need to convert it to Int, String, regardless of what it will be there, even if the column is visible = false.

0
source

In the rowDataBound event, you can access the field value using something like:

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

even if your visibility is set to false.

0
source

Source: https://habr.com/ru/post/920494/


All Articles