How to remove/make invisible autogenerated gridview column

  • Thread starter Thread starter Julia B
  • Start date Start date
J

Julia B

Hi

I've just moved from asp.net 1.1 to 2.0 so am new to gridviews and have a
problem.

I've got a gridview on a form in a web layer project. It has an
objectdatasource object which calls a method in a class in a seperate data
layer project.Therefore the columns are autogenerated. The problem I've got
is that the data that fills the gridview has an "id" column which I don't
want to display but I do want to use programmatically. I've tried hiding &
removing the column but, wherever I put the code, I get an "Argument out of
range" error. My code is:

Me.UserGridView.Columns.RemoveAt(1)

I've tried this in Page Load, Page LoadComplete etc etc.... to no avail.
There is no code on the page to populate the GridView, it's all done through
VisualStudio's ObjectDataSource object properties.

Any clues on how to resolve this would be greatly appreciated.

Thanks
Julia
 
You can do it in RowCreated event. Don't remove columns, just hide them with
e.Item.Cells(1).Visible = false.

In any case, put "id" in DataKeyNames, otherwise you won't find in in
postbacks.
 
Thanks for this. Unfortunately I still get the same error.

I had to use e.Row.Cells(1).Visible = False

Item is not a member of the argument.

Any ideas where I'm going wrong?

Julia
Eliyahu Goldin said:
You can do it in RowCreated event. Don't remove columns, just hide them with
e.Item.Cells(1).Visible = false.

In any case, put "id" in DataKeyNames, otherwise you won't find in in
postbacks.

--
Eliyahu Goldin,
Software Developer
Microsoft MVP [ASP.NET]
http://msmvps.com/blogs/egoldin


Julia B said:
Hi

I've just moved from asp.net 1.1 to 2.0 so am new to gridviews and have a
problem.

I've got a gridview on a form in a web layer project. It has an
objectdatasource object which calls a method in a class in a seperate data
layer project.Therefore the columns are autogenerated. The problem I've
got
is that the data that fills the gridview has an "id" column which I don't
want to display but I do want to use programmatically. I've tried hiding &
removing the column but, wherever I put the code, I get an "Argument out
of
range" error. My code is:

Me.UserGridView.Columns.RemoveAt(1)

I've tried this in Page Load, Page LoadComplete etc etc.... to no avail.
There is no code on the page to populate the GridView, it's all done
through
VisualStudio's ObjectDataSource object properties.

Any clues on how to resolve this would be greatly appreciated.

Thanks
Julia
 
Back
Top