Click here to Skip to main content
15,883,705 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
i am displaying 4 columns in a grid view in my web form. when i click edit it comes in edit mode. but i dont need the Employee_ID column to become editable. how too set this. pls someone resolve coding in asp is given below..
<asp:GridView ID="gridRegistrationTableDetails" runat="server" 
                                OnRowEditing="EditRecord "
OnRowCancelEdit="CancelRecord "
OnRowDeleting="DeleteRecord "
                                    BackColor="White" BorderColor="#336666" BorderStyle="Double" BorderWidth="3px" 
                                    CellPadding="4" GridLines="Horizontal" onrowcancelingedit="CancelRecord">
                                        <asp:CommandField ShowEditButton="True" CausesValidation="False" />
                                        <asp:CommandField ShowDeleteButton="True" />
                                    <FooterStyle BackColor="White" ForeColor="#333333" />
                                    <HeaderStyle BackColor="#336666" Font-Bold="True" ForeColor="White" />
                                    <PagerStyle BackColor="#336666" ForeColor="White" HorizontalAlign="Center" />
                                    <RowStyle BackColor="White" ForeColor="#333333" />
                                    <SelectedRowStyle BackColor="#339966" Font-Bold="True" ForeColor="White" />
                                    <SortedAscendingCellStyle BackColor="#F7F7F7" />
                                    <SortedAscendingHeaderStyle BackColor="#487575" />
                                    <SortedDescendingCellStyle BackColor="#E5E5E5" />
                                    <SortedDescendingHeaderStyle BackColor="#275353" />
Updated 24-Jul-12 22:04pm

1 solution

Two ways:
1. In ASPX if the columns are defined:
<asp:BoundField DataField="MyProperty" HeaderText="Int" InsertVisible="False" ReadOnly="True" />

2. In code behind file:
To make the column readonly on editing add this
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
   // Find the column and set it readonly
   BoundField bound = GridView1.Columns[1] as BoundField;
   bound.InsertVisible = false;
   bound.ReadOnly = true;

Generally, we define the columns of a grid and option 1 should do just fine.
Share this answer
sandeep nagabhairava 25-Jul-12 7:34am    
my 5!...
Diego Chicaiza 12-Oct-17 9:23am    
Thanks!!! :D

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900