C# – How to hide a column but still access its value

asp.netcgridview

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=" + "");

Best Answer

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.

Related Topic