I've had a play with this, and there is, indeed a way of disabling the "edit" column programatically.
This page will allow users access to the editing funcions as long as they are in the "Admins" role...
First the .aspx file...
1 <%@ Page Language="VB" AutoEventWireup="false" CodeFile="DisableEdit.aspx.vb" Inherits="DisableEdit" %>
2
3 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
4
5 <html xmlns="http://www.w3.org/1999/xhtml">
6 <head runat="server">
7 <title>Untitled Page</title>
8 </head>
9 <body>
10 <form id="form1" runat="server">
11 <div>
12 <asp:LoginView ID="LoginView1" runat="server">
13 <loggedintemplate>
14 Welcome,
15 <asp:LoginName ID="LoginName1" runat="server" />
16 <br />
17 <br />
18 <br />
19 <asp:LoginStatus ID="LoginStatus1" runat="server" />
20 </loggedintemplate>
21 <AnonymousTemplate>
22 <asp:Login ID="Login1" runat="server">
23 </asp:Login>
24 </AnonymousTemplate>
25 </asp:LoginView>
26 <br />
27 <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="CityID"
28 DataSourceID="srcCity" OnRowDataBound="cityRowDatabound">
29 <Columns>
30 <asp:CommandField visible="false" ShowEditButton="True"></asp:CommandField>
31 <asp:BoundField DataField="CityNaam" HeaderText="City" SortExpression="CityNaam"></asp:BoundField>
32 <asp:TemplateField HeaderText="Country">
33
34 <EditItemTemplate>
35 <asp:DropDownList ID="ddl1" runat="server" DataSourceID="srcCountry" DataTextField="CountryNaam"
36 DataValueField="CountryID" SelectedValue='<%# Bind("CountryID") %>'>
37 </asp:DropDownList>
38 </EditItemTemplate>
39
40 <ItemTemplate>
41 <asp:Label ID="lbl1" runat="server" Text=""></asp:Label>
42 </ItemTemplate>
43
44 </asp:TemplateField>
45 </Columns>
46 </asp:GridView>
47 <asp:SqlDataSource ID="srcCity" runat="server" ConnectionString="<%$ ConnectionStrings:TestConnectionString %>"
48 SelectCommand="SELECT * FROM [tblCity]" ConflictDetection="CompareAllValues"
49 DeleteCommand="DELETE FROM [tblCity] WHERE [CityID] = @original_CityID AND [CountryID] = @original_CountryID AND [CityNaam] = @original_CityNaam"
50 InsertCommand="INSERT INTO [tblCity] ([CountryID], [CityNaam]) VALUES (@CountryID, @CityNaam)"
51 UpdateCommand="UPDATE [tblCity] SET [CountryID] = @CountryID, [CityNaam] = @CityNaam WHERE [CityID] = @original_CityID AND [CountryID] = @original_CountryID AND [CityNaam] = @original_CityNaam"
52 OldValuesParameterFormatString="original_{0}">
53 <DeleteParameters>
54 <asp:Parameter Name="original_CityID" Type="Int16" />
55 <asp:Parameter Name="original_CountryID" Type="Int16" />
56 <asp:Parameter Name="original_CityNaam" Type="String" />
57 </DeleteParameters>
58 <UpdateParameters>
59 <asp:Parameter Name="CountryID" Type="Int16" />
60 <asp:Parameter Name="CityNaam" Type="String" />
61 <asp:Parameter Name="original_CityID" Type="Int16" />
62 <asp:Parameter Name="original_CountryID" Type="Int16" />
63 <asp:Parameter Name="original_CityNaam" Type="String" />
64 </UpdateParameters>
65 <InsertParameters>
66 <asp:Parameter Name="CountryID" Type="Int16" />
67 <asp:Parameter Name="CityNaam" Type="String" />
68 </InsertParameters>
69 </asp:SqlDataSource>
70 <asp:SqlDataSource ID="srcCountry" runat="server" ConnectionString="<%$ ConnectionStrings:TestConnectionString %>"
71 SelectCommand="SELECT * FROM [tblCountry]" />
72 </div>
73 </form>
74 </body>
75 </html>
76
and now the codebehind...
1 Imports System.Data
2
3 Partial Class DisableEdit
4 Inherits System.Web.UI.Page
5
6 Protected Sub cityRowDatabound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
7 Dim lbl1 As Label
8 If e.Row.RowType = DataControlRowType.DataRow Then
9
10 If e.Row.RowState = DataControlRowState.Normal Or _
11 e.Row.RowState = DataControlRowState.Alternate Then
12 lbl1 = CType(e.Row.FindControl("lbl1"), Label)
13 countryData.RowFilter = "CountryID = " & CType(e.Row.DataItem, DataRowView)("CountryId").ToString()
14 lbl1.Text = CStr(countryData.Item(0).Row.Item("CountryNaam"))
15 End If
16
17 End If
18 End Sub
19
20
21 Dim countryData As DataView
22 ' this DataView will hold all of the countries, loaded at Page_Load
23
24 Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
25 ' Load all of the countries into a DataView from the SqlDataSource
26 countryData = CType(srcCountry.Select(DataSourceSelectArguments.Empty), DataView)
27
28 If Roles.IsUserInRole(User.Identity.Name, "Admins") Then
29 GridView1.Columns(0).Visible = "true"
30 End If
31
32 End Sub
33
34 End Class
35
Notice that on line 30 of the .aspx file, I set the "visible" attribute of the "CommandField" column to FALSE by default.
Then, on line 28/29 of the codebehind, I set it back to TRUE if the user is in the "Admins" role...
To see this in action click on http://liberator.boldlygoingnowhere.org/PlaySite/DisableEdit.aspx Username is "Admin", password is "Administrator!" (Don't forget that exclamation mark/point)
IMHO, this is a much neater way than the other ways suggested (including my own earlier suggestoins!).
(Just realised you were asking about C#, and my example is in VB - I'll have a look at translating it in the morning...)
(I put the wrong password in by mistake - I've corrected it now)