[c#] How to hide a column (GridView) but still access its value?

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

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

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

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

This question is related to c# asp.net gridview hide

The answer is


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;

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

.col0
{
  display: none;
}

And in the aspx file where you add the column you should specify the CSS properties:

<asp:BoundField HeaderText="Address Key" DataField="Address_Id" ItemStyle-CssClass="col0" HeaderStyle-CssClass="col0" > </asp:BoundField>

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

In the .aspx file set the GridView property

DataKeyNames = "Outlook_ID"

Now, in an event handler you can access the value of this key like so:

grid.DataKeys[rowIndex]["Outlook_ID"]

This will give you the id at the specified rowindex of the grid.


You can do it code behind.

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


If you do have a TemplateField inside the columns of your GridView and you have, say, a control named blah bound to it. Then place the outlook_id as a HiddenField there 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, grab the row in the event you want the outlook_id and then access the control.
For RowDataBound access it like:

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

Do get back, if you have trouble accessing the clicked row. And don't forget to mention the event at which you would like to access that.


Leave visible columns before filling the GridView. Fill the GridView and then hide the columns.


When I want access some value from GridView before GridView was appears.

  1. I have a BoundField and bind DataField nomally.
  2. In RowDataBound event, I do some process in that event.
  3. Before GridView was appears I write this:

    protected void GridviewLecturer_PreRender(object sender, EventArgs e) 
    {
        GridviewLecturer.Columns[0].Visible = false;
    }
    

You can make the column hidden on the server side and for some reason this is different to doing it the aspx code. It can still be referenced as if it was visible. Just add this code to your 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;
    }
}

You can do it programmatically:

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

In this way you set the column to visible before databinding, so the column is generated. The you set the column to not visible, so it is not displayed.


Here is how to get the value of a hidden column in a GridView that is set to Visible=False: add the data field in this case SpecialInstructions to the DataKeyNames property of the bound GridView , and access it this way.

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

That's it, it works every time very simple.


<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);
}

Define a style in css:

.hiddencol { display: none; }

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

<asp:BoundField DataField="ID" HeaderText="ID" ItemStyle-CssClass="hiddencol"  HeaderStyle-CssClass="hiddencol" ClientIDMode="Static" />

Examples related to c#

How can I convert this one line of ActionScript to C#? Microsoft Advertising SDK doesn't deliverer ads How to use a global array in C#? How to correctly write async method? C# - insert values from file into two arrays Uploading into folder in FTP? Are these methods thread safe? dotnet ef not found in .NET Core 3 HTTP Error 500.30 - ANCM In-Process Start Failure Best way to "push" into C# array

Examples related to asp.net

RegisterStartupScript from code behind not working when Update Panel is used You must add a reference to assembly 'netstandard, Version=2.0.0.0 No authenticationScheme was specified, and there was no DefaultChallengeScheme found with default authentification and custom authorization How to use log4net in Asp.net core 2.0 Visual Studio 2017 error: Unable to start program, An operation is not legal in the current state How to create roles in ASP.NET Core and assign them to users? How to handle Uncaught (in promise) DOMException: The play() request was interrupted by a call to pause() ASP.NET Core Web API Authentication Could not load file or assembly 'CrystalDecisions.ReportAppServer.CommLayer, Version=13.0.2000.0 WebForms UnobtrusiveValidationMode requires a ScriptResourceMapping for jquery

Examples related to gridview

Simple Android grid example using RecyclerView with GridLayoutManager (like the old GridView) Android Recyclerview GridLayoutManager column spacing Get the cell value of a GridView row add new row in gridview after binding C#, ASP.net Check if list is empty in C# How to refresh Gridview after pressed a button in asp.net Putting GridView data in a DataTable How to get row data by clicking a button in a row in an ASP.NET gridview Change header text of columns in a GridView Twitter Bootstrap and ASP.NET GridView

Examples related to hide

bootstrap 4 responsive utilities visible / hidden xs sm lg not working jQuery get the name of a select option jQuery select change show/hide div event Hide Show content-list with only CSS, no javascript used Permanently hide Navigation Bar in an activity How can I hide/show a div when a button is clicked? jQuery hide and show toggle div with plus and minus icon Hide Text with CSS, Best Practice? Show div #id on click with jQuery JavaScript - Hide a Div at startup (load)