How to hide a column (GridView) but still access its value?
Asked Answered
S

16

85

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.

Shadshadberry answered 21/3, 2011 at 10:21 Comment(1)
Check this: codingfusion.com/Post/…Rozalin
G
45

If I am not mistaken, GridView does not hold the values of BoundColumns that have the attribute visible="false". Two things you may do here, one (as explained in the answer from V4Vendetta) to use Datakeys. Or you can change your BoundColumn to a TemplateField. And in the ItemTemplate, add a control like Label, make its visibility false and give your value to that Label.

Godbey answered 21/3, 2011 at 10:31 Comment(3)
This hides the column but it doesn't hide the Column header. How will you do that?Lowminded
Just add <HeaderStyle CssClass="hidden" /> in your boundfield. And define a css style like this: .hidden { display:none; }Trickle
Hiding the column header along with the column is a huge part of this. If you do not hide it, all the columns shift out of place. Thanks!Wacker
D
90
<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);
}
Dulcedulcea answered 21/5, 2012 at 6:39 Comment(3)
See netomatix.com/development/GridViewHideColumn.aspx for an in-depth explanation of this solution.Ruffianism
This solution has the added advantage that the data is still accessible in javascript on client side also. Should be the preferred solution to this problem!Hoick
You are a legend!Filaria
R
47

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" />
Rennet answered 10/11, 2012 at 18:13 Comment(1)
This is a much more effective answerLavernelaverock
G
45

If I am not mistaken, GridView does not hold the values of BoundColumns that have the attribute visible="false". Two things you may do here, one (as explained in the answer from V4Vendetta) to use Datakeys. Or you can change your BoundColumn to a TemplateField. And in the ItemTemplate, add a control like Label, make its visibility false and give your value to that Label.

Godbey answered 21/3, 2011 at 10:31 Comment(3)
This hides the column but it doesn't hide the Column header. How will you do that?Lowminded
Just add <HeaderStyle CssClass="hidden" /> in your boundfield. And define a css style like this: .hidden { display:none; }Trickle
Hiding the column header along with the column is a huge part of this. If you do not hide it, all the columns shift out of place. Thanks!Wacker
C
38

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.

Cobwebby answered 21/3, 2011 at 10:27 Comment(2)
DataKeys where meant for thatIvon
How would this work if you were using DataSource controls with CRUD code in the HTML...?Krystakrystal
M
26

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.

Mixtec answered 11/11, 2011 at 11:16 Comment(3)
Not working at all, I get ArrayOutOfBoundsException when I place that code in a Panel PreRender method.Satinwood
Worked for me simple and easy.Hypaethral
This is by far the best answer.Detail
O
12

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.

Osi answered 21/3, 2011 at 10:44 Comment(0)
M
9

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;
    }
}
Margarettmargaretta answered 20/4, 2012 at 15:5 Comment(1)
This works. For everyone looking you need to make sure both the header column and the grid view column are hidden.Lease
G
3

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

Glisten answered 11/10, 2011 at 20:40 Comment(0)
G
2

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

.col0
{
  display: none !important;
}

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>
Giesecke answered 16/2, 2015 at 6:13 Comment(0)
D
2

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.

Decortication answered 27/4, 2015 at 16:14 Comment(0)
S
1

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;
Smokeless answered 25/6, 2012 at 16:10 Comment(0)
S
1

I found this solution, an elegant(IMO) quick 2 parter.

1. On your gridview, add a column as normal, no need to do anything differently:

<asp:BoundField DataField="AccountHolderName" HeaderText="Account Holder" 
 ReadOnly="true"/>  

You can keep the header text if this is useful to you for later processing.

2. In the rowdatabound method for the grid hide the header, footer and row for that column index:

if (e.Row.RowType == DataControlRowType.Header)
{
    e.Row.Cells[10].Visible = false;
}
if (e.Row.RowType == DataControlRowType.Footer)
{
    e.Row.Cells[10].Visible = false;
}
if (e.Row.RowType == DataControlRowType.DataRow)
{                                   
    e.Row.Cells[10].Visible = false;
}
 
Scarletscarlett answered 27/1, 2023 at 10:55 Comment(0)
F
0

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!!

Favien answered 19/10, 2015 at 7:9 Comment(0)
S
0

I searched a lot but no luck. The most of them was working with some errors. Finally I used this code and it worked for me. probably you should change 1 to some other value. for me I would hide second col.

protected void FoundedDrGridView_RowDataBound(object sender, GridViewRowEventArgs e) {

    if(e.Row.RowType!=DataControlRowType.Pager)e.Row.Cells[1].Visible = false;
}
Scholiast answered 20/9, 2021 at 17:55 Comment(0)
P
0

I just set the width to 0. and it works.

columns.AddFor(m => m.Id).Name("hide-id").Width(0);
Pomeranian answered 8/7, 2022 at 7:17 Comment(0)
E
-1

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;
    }
    
Effusion answered 11/3, 2016 at 9:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.