How Do I Conditionally Remove An EditCommandColumn?

D

DaveC

Hi,

I have a DataGrid control that "by default" comes up with an
EditCommandColumn. However, I don't want all users to see this. How
do I conditionally remove the EditCommandColumn? Thanks in advance for
your help.
 
B

Brock Allen

In Page_Load (or somewhere similar) do this:

if (!User.IsInRole("TheRoleYouCareAbout"))
{
grid.Columns[TheColumnIndexYouCareAbout].Visible = false;
}
 
D

DaveC

Thanks, but the problem is that an index of 0 is the first "real"
column, not the EditCommandColumn.
 
B

Brock Allen

Thanks, but the problem is that an index of 0 is the first "real"
column, not the EditCommandColumn.

Hmm, I'd have to go look into why this is. But for a quick alternative answer,
you can always create your own TemplateColumn, add the <asp:Button runat=server
CommandName="Edit" ... /> and then dynamically set it Visible=false.
 
D

DaveC

Brock,

Thank you very much for your help. My statement about 0 being the
index of the first "real" column was based on the fact that I was
indexing off of a DataColumnCollection instead of the DataGrid itself!
I believe that I'm all set now by doing just what you originally said.
Again, thank you very much!
 

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,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top