tags:

views:

155

answers:

2

How do I make a column invisible in a gridview? I tried to use this:

dataGridView.Columns(0).Visible = False

But its getting an error "Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index". How can I do this?

+1  A: 

Finally, I got the answer. Using this we can make a column invisible:

Private Sub dataGridView_RowDataBound( _
   ByVal sender As Object, _
   ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs _
) Handles dataGridView.RowDataBound
   If e.Row.RowType = DataControlRowType.DataRow OrElse _
      e.Row.RowType = DataControlRowType.Header OrElse _
      e.Row.RowType = DataControlRowType.Footer _
   Then
      e.Row.Cells(1).Visible = False
   End If
End Sub
Joby Kurian
Ouch, that is a painful way to do it... try getting the column in the PreRender event of your page and setting its visibility then.
slugster
do you need the `if` statement?
Matt Joslin
A: 

I can not understand why is not working with you.

Its working just find on me - at least on c#.

Check to see what is going wrong, maybe you call it before the DataGrid is rendered/created or something like that.

Aristos