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 this...my coding in asp is given below..
ASP.NET
<asp:GridView ID="gridRegistrationTableDetails" runat="server" 
                                OnRowEditing="EditRecord "
OnRowCancelEdit="CancelRecord "
OnRowUpdating="UpdateRecord"
OnRowDeleting="DeleteRecord "
                               onselectedindexchanged="gridRegistrationTableDetails_SelectedIndexChanged" 
                                    BackColor="White" BorderColor="#336666" BorderStyle="Double" BorderWidth="3px" 
                                    CellPadding="4" GridLines="Horizontal" onrowcancelingedit="CancelRecord">
                                    <Columns>
                                        <asp:CommandField ShowEditButton="True" CausesValidation="False" />
                                        <asp:CommandField ShowDeleteButton="True" />
                                    </Columns>
                                    <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" />
                                </asp:GridView>
Posted
Updated 24-Jul-12 22:04pm
v2

1 solution

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


2. In code behind file:
To make the column readonly on editing add this
C#
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
 
Comments
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