user3708917
user3708917

Reputation:

The best way to conceal DataKeyName column

There are many ways to hide a column but how do you hide one in GridView and retaic access to values in that column?

Upvotes: 1

Views: 53

Answers (2)

ArtK
ArtK

Reputation: 1185

The best way includes 3 steps. 1. Assign DataKeyName in Grid definition. 2. Set Visible to false in column definition - that will prevent sending values to the browser. 3. Use SelectedIndexChanging event of Grid view. As this event is fired long begore PageUnload the data in column still accessible. You can store that value in ViewState, Session or just pass it to another method.

protected void GridView1_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
    {
        if (e.NewSelectedIndex != -1)
        {
            ViewState.Add("myKeyValue", GridView1.DataKeys[e.NewSelectedIndex].Value);

        }

Upvotes: 0

Philip Pittle
Philip Pittle

Reputation: 12295

After databinding set the Column's Visible property to false.

Upvotes: 1

Related Questions