Tassisto
Tassisto

Reputation: 10345

How to hide a column but still access its value?

I have a gridview, with some columns. I want to hide one column, but still access its value when I select a record.

Could someone help me to achieve this?

Any Help is appreciated.

This is my gridview: OutlookID is the column to hide! <asp:GridView ID="gvOutlookMeldingen" runat="server" AllowSorting="True" AutoGenerateColumns="False" AutoGenerateSelectButton="True" onselectedindexchanged="GridView_SelectedIndexChanged"> <Columns> <asp:BoundField DataField="Melder" HeaderText="Melder" /> <asp:BoundField DataField="Onderwerp" HeaderText="Onderwerp" /> <asp:TemplateField HeaderText="Omschrijving"> <ItemTemplate> <div style="overflow:auto; width: 500px; height: 150px;"> <asp:Label ID="lblOmschrijving" runat="server" Text='<%# Bind("Omschrijving")%>'></asp:Label> </div> </ItemTemplate> </asp:TemplateField> <asp:BoundField DataField="Meldingsdatum" HeaderText="Meldingsdatum" /> <asp:BoundField DataField="OutlookID" HeaderText="OutlookID" Visible="false" /> </Columns> </asp:GridView>

This is the code when I select a record:

Label lblOmschrijving = (Label)gvOutlookMeldingen.SelectedRow.FindControl("lblOmschrijving");
            //Label lblOutlookID = (Label)gvOutlookMeldingen.SelectedRow.FindControl("lblOutlookID");

            Response.Redirect("Detailscherm.aspx?"
                + "melder=" + Server.UrlEncode(gvOutlookMeldingen.SelectedRow.Cells[1].Text)
                + "&meldingsdatum=" + gvOutlookMeldingen.SelectedRow.Cells[4].Text
                + "&onderwerp=" + Server.UrlEncode(gvOutlookMeldingen.SelectedRow.Cells[2].Text)
                + "&outlookid=" + Server.UrlEncode(gvOutlookMeldingen.SelectedRow.Cells[5].Text)
                + "&omschrijving=" + Server.UrlEncode(lblOmschrijving.Text)
                + "&niv1=" + ""
                + "&niv2=" + "");

Upvotes: 0

Views: 6466

Answers (6)

Fabzien
Fabzien

Reputation: 124

I had the same issue.

You can't hide the column and keep value in code behind.

You have to hide it directly on client side with a javascript.

I did that :

On my css or page :

<style type="text/css">
.hiddencol
{
    display: none;
}
.viscol
{
    display: block;
}
</style>

Then add style into the BoundField of the gridViewer.

For example :

        <asp:BoundField DataField="AgentGUID" HeaderText="AgentGUID" ReadOnly="True" SortExpression="AgentGUID"
            meta:resourcekey="BoundFieldResource1">
            <HeaderStyle CssClass="hiddencol" />
            <ItemStyle CssClass="hiddencol" />
            <FooterStyle CssClass="hiddencol" />
        </asp:BoundField>

Upvotes: 0

sushil pandey
sushil pandey

Reputation: 762

when we set visiblity of control false in Design time that will be not render .Try to set visiblity =false in gridView rowCreated Event .in below code i am setting second column visibility= false

protected void grid_RowCreated(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { e.Row.Cells[2].Visible = false;

        }
        if (e.Row.RowType == DataControlRowType.Header)
        {
            e.Row.Cells[2].Visible = false;

        }
        if (e.Row.RowType == DataControlRowType.Footer)
        {
            e.Row.Cells[2].Visible = false;

        }
    }

and now try to Get Value .Surely you will get the value.

Upvotes: 0

apacay
apacay

Reputation: 1712

You can also set it invisible at the client side. With Javascript.

document.getElementById(myObject).visible = "false";

Upvotes: 0

Lost in Alabama
Lost in Alabama

Reputation: 1653

Set this code after you've binded the data. To get this functionality I do this:

MyGridView.Columns[0].visible = true;
MyGridView.DataBind();
MyGridView.Columns[0].visible = false;

With this the first column is hidden, but you should be able to acces it's value.

Upvotes: 4

eugeneK
eugeneK

Reputation: 11116

Create Template column instead of your SELECT button. Set

PostbackUrl='<%# Eval("somepage.aspx?id={0}","wanted column") %>'

. Remove column via designer.

Upvotes: 0

Ian Pugsley
Ian Pugsley

Reputation: 1062

If you don't want the data to be available on the client side, you'll have to set the server-side Visible = "False" property of whatever DataControlField you're using (preferably in the markup). You'll still be able to access the column from the server side.

You might want to consider using the GridView's DataKeys property - it might be more suited to your needs.

Upvotes: 0

Related Questions