Link to home
Start Free TrialLog in
Avatar of keymac
keymacFlag for Australia

asked on

How do I hide a column of a Gridview at runtime

I am creating a gridview at runtime but don't want all the columns to be shown, ie the key information for the row of data I am showing.  I know I can do this

gridview1.column.item(0).visible = false

but although the column is not shown it also does not have the data in the column either and since I need to know the key data for processing the selected item from the grid, the above does not work for me
Avatar of Rahul Agarwal
Rahul Agarwal
Flag of India image

Right this code in the GridView1_RowDataBound Event and hide the column. In Controls(6) the 6 is number of column to be Hide on runtime.

If e.Row.RowType = DataControlRowType.Header Then
         e.Row.Controls(6).Visible = False
        End If
        If e.Row.RowType = DataControlRowType.DataRow Then
         e.Row.Controls(6).Visible = False
       End If
To hide a column at runt time you can hook up to the GridView’s RowDataBound event and set the Visible property of the cell you want to hide to false:   protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) { e.Row.Cells[0].Visible = false; }...
Avatar of keymac

ASKER

As I said in the question, that approach dos not work.  I need the column to be hidden not invisible.  If it is invisible it is not available as data in the grid, ie I want the column to still have data bound to it but to be hidden to the viewer.
have you tried this

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) { e.Row.Cells[0].Visible = false;
ASKER CERTIFIED SOLUTION
Avatar of Obadiah Christopher
Obadiah Christopher
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
in vb.net

Protected Sub GridView1_RowDataBound(sender As Object, e As GridViewRowEventArgs)
      e.Row.Cells(0).Visible = False
End Sub
Just use the Datakeyname property of the gridview to store the value of key u use 4 processing the row.
Avatar of keymac

ASKER

Magnificent