Last post Feb 07, 2016 04:18 AM by a2h
Member
6 Points
10 Posts
Feb 07, 2016 03:20 AM|talabalamalahala|LINK
I want to set a gridview to read only unless a userid = 12 I know you can use the HTML of the grid to set readonly, but how can I specify readonly in the C# code behind for a gridview?
All-Star
50061 Points
9685 Posts
Feb 07, 2016 04:18 AM|A2H|LINK
talabalamalahala but how can I specify readonly in the C# code behind for a gridview?
You can try with the below code
protected void GridView1_RowDataBound1(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { ((BoundField)GridView1.Columns[0]).ReadOnly = true; ((BoundField)GridView1.Columns[1]).ReadOnly = true; ((BoundField)GridView1.Columns[2]).ReadOnly = true; ((BoundField)GridView1.Columns[3]).ReadOnly = true; //You can add other column here } }
Complete Code
HTML
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" AllowSorting="True" DataSourceID="SqlDataSource2" AutoGenerateEditButton="True" OnRowDataBound="GridView1_RowDataBound1" OnRowEditing="GridView1_RowEditing"> <Columns> <asp:BoundField DataField="AddressID" HeaderText="AddressID" /> <asp:BoundField DataField="AddressLine1" HeaderText="AddressLine1" /> <asp:BoundField DataField="City" HeaderText="City" /> <asp:BoundField DataField="StateProvinceID" HeaderText="StateProvinceID" /> <asp:BoundField DataField="PostalCode" HeaderText="PostalCode" /> <asp:BoundField DataField="ModifiedDate" HeaderText="ModifiedDate" /> </Columns> </asp:GridView> <asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:AdventureWorks2008R2ConnectionString2 %>" SelectCommand="SELECT top 20 [AddressID], [AddressLine1],City,StateProvinceID,PostalCode,ModifiedDate FROM Person.Address"></asp:SqlDataSource>
C#:
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e) { //Your editing code }
Sample Demo
Member
6 Points
10 Posts
Set Grid To Read Only
Feb 07, 2016 03:20 AM|talabalamalahala|LINK
I want to set a gridview to read only unless a userid = 12 I know you can use the HTML of the grid to set readonly, but how can I specify readonly in the C# code behind for a gridview?
All-Star
50061 Points
9685 Posts
Re: Set Grid To Read Only
Feb 07, 2016 04:18 AM|A2H|LINK
You can try with the below code
Complete Code
HTML
C#:
Sample Demo
A2H
My Blog | Dotnet Funda