GRIDVIEW: changing column attributes

G

Guest

I have a gridview with some boundfields and buttonfields, and based on the
user's role I want to modify their attributes: for example, if the person's
role doesnt allow updates, I want to set a buttonfields visible attribute to
"false". I know how to get data values from a clicked column, but I havent
been able to find out how to modify the columns such as in the above scenario
when the gridview loads.
Any help is very appreciated.
 
G

Guest

If you use a TemplateField instead of a BoundField for the columns that you
wish to change its visibility based on the user role then you can write
something like this:
<asp:TemplateField>
<ItemTemplate>
<asp:Label ID="Label1" runat="server"
Text='<%# Bind("OrderDate", "{0:d}") %>'
Visible='<%=SomeProtectedFunctionThatReturnsBool()'></asp:Label>
</ItemTemplate>

where SomeProtectedFunctionThatReturnsBool would be a function in your
codebehind that decide whether the user has access of not.
 
R

Rob Roberts

Mark,

In addition to Phillip's answer, I think that you could do something like
this: Set up a RowDataBound event handler for your GridView. You can then
set the visible attribute of a buttonfield like this:

Button button = (Button)e.Row.Cells[1].Controls[0];
button.Attributes.Add("visible", "false");

Of course, the index that you use to access the right cell will vary
depending on the layout of your grid. I used Cells[1] just as an example.

--Rob Roberts
 

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

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,073
Latest member
DarinCeden

Latest Threads

Top