How to remove/make invisible autogenerated gridview column

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
 
E

Eliyahu Goldin

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.
 
J

Julia B

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top