How to hide a column (GridView), but still access its value?

I have a GridView with a DataSource (SQL Database). I want to hide the column, but still can access the value when selecting a record. Can someone show me how to do this?

This is the column that I want to hide and want to access its value:

 <asp:BoundField DataField="Outlook_ID" HeaderText="OutlookID" /> 

I tried everything to hide the column (property Visible="false" ), but I cannot access its value.

+58
c # hide gridview
Mar 21 '11 at 10:21
source share
13 answers

If I'm not mistaken, the GridView does not contain a BoundColumns value that has the visible="false" attribute. Here you can do two things: one (as explained in V4Vendetta's answer) to use Datakeys . Or you can change the BoundColumn to a TemplateField . And in the ItemTemplate add a control like Label , make its visibility false, and specify the Label value.

+39
Mar 21 '11 at 10:31
source share
 <head runat="server"> <title>Accessing GridView Hidden Column value </title> <style type="text/css"> .hiddencol { display: none; } </style> <asp:BoundField HeaderText="Email ID" DataField="EmailId" ItemStyle-CssClass="hiddencol" HeaderStyle-CssClass="hiddencol" > </asp:BoundField> ArrayList EmailList = new ArrayList(); foreach (GridViewRow itemrow in gvEmployeeDetails.Rows) { EmailList.Add(itemrow.Cells[YourIndex].Text); } 
+65
May 21 '12 at 6:39 a.m.
source share

Define a style in css:

 .hiddencol { display: none; } 

Then add the attribute ItemStyle-CssClass="hiddencol" and HeaderStyle-CssClass="hiddencol" in the grid field:

 <asp:BoundField DataField="ID" HeaderText="ID" ItemStyle-CssClass="hiddencol" HeaderStyle-CssClass="hiddencol" ClientIDMode="Static" /> 
+31
Nov 10 '12 at 18:13
source share

You can use DataKeys to get the value of such fields, because (as you said), when you set the normal BoundField to visible false, you cannot get their value.

In the .aspx file, set the GridView property

 DataKeyNames = "Outlook_ID" 

Now in the event handler you can access the value of this key as follows:

 grid.DataKeys[rowIndex]["Outlook_ID"] 

This will give you the id in the specified rowindex grid.

+27
Mar 21 '11 at 10:27
source share

You can do this programmatically:

 grid0.Columns[0].Visible = true; grid0.DataSource = dt; grid0.DataBind(); grid0.Columns[0].Visible = false; 

This way you set the column visible before data binding, so a column is generated. You have determined that the column is not displayed, therefore it is not displayed.

+21
Nov 11 '11 at 11:16
source share

If you have a TemplateField inside the columns of your GridView , and you have, say, a control named blah associated with it. Then put outlook_id as a HiddenField like this:

 <asp:TemplateField HeaderText="OutlookID"> <ItemTemplate> <asp:Label ID="blah" runat="server">Existing Control</asp:Label> <asp:HiddenField ID="HiddenOutlookID" runat="server" Value='<%#Eval("Outlook_ID") %>'/> </ItemTemplate> </asp:TemplateField> 

Now take the line in case you want outlook_id and then access the control.
For RowDataBound use it like:

 string outlookid = ((HiddenField)e.Row.FindControl("HiddenOutlookID")).Value; 

Come back if you have problems accessing the pressed row. And do not forget to indicate the event in which you would like to access.

+10
Mar 21 '11 at 10:44
source share

You can make the column hidden on the server side, and for some reason this is different from aspx code. It can still be referenced as if it were visible. Just add this code to the OnDataBound event.

 protected void gvSearchResults_DataBound(object sender, EventArgs e) { GridView gridView = (GridView)sender; if (gridView.HeaderRow != null && gridView.HeaderRow.Cells.Count > 0) { gridView.HeaderRow.Cells[UserIdColumnIndex].Visible = false; } foreach (GridViewRow row in gvSearchResults.Rows) { row.Cells[UserIdColumnIndex].Visible = false; } } 
+7
Apr 20 '12 at 15:05
source share

Leave visible columns before populating the GridView . Fill in the GridView and then hide the columns.

+4
Oct 11 '11 at 20:40
source share

I used a method similar to user496892:

 SPBoundField hiddenField = new SPBoundField(); hiddenField.HeaderText = "Header"; hiddenField.DataField = "DataFieldName"; grid.Columns.Add(hiddenField); grid.DataSource = myDataSource; grid.DataBind(); hiddenField.Visible = false; 
+1
Jun 25 '12 at 16:10
source share

I have a new solution, hide the client side column using css or javascript or jquery .

 .col0 { display: none; } 

And set gvClientDetails.Columns[0].Visible = true; If set to false .

+1
Feb 16 '15 at 6:13
source share

Here's how to get the value of the hidden column in the GridView that is set to Visible=False : add the data field in this case SpecialInstructions to the DataKeyNames property of the associated GridView and access the path to it.

 txtSpcInst.Text = GridView2.DataKeys(GridView2.SelectedIndex).Values("SpecialInstructions") 

What is he, he works every time is very simple.

+1
Apr 27 '15 at 16:14
source share

You can do it with code.

Set visible= false for columns after data binding. After that, you can again see the visibility " true " in the row_selection function from the grid view. It will work!

0
Oct 19 '15 at 7:09
source share

When do I need to access some value from a GridView until a GridView appears.

  • I have a BoundField and a BoundField binding.
  • In the RowDataBound event RowDataBound I am doing some kind of process in this case.
  • Before the appearance of GridView I write the following:

     protected void GridviewLecturer_PreRender(object sender, EventArgs e) { GridviewLecturer.Columns[0].Visible = false; } 
0
Mar 11 '16 at 9:41
source share



All Articles