This is a migrated thread and some comments may be shown as answers.

How to make a column read-only except for the new record

2 Answers 93 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Chad Hensley
Top achievements
Rank 1
Chad Hensley asked on 19 Apr 2010, 10:44 PM

Requirements

RadControls version

2010.1.10.409
.NET version 4.0
Visual Studio version

2010
programming language

C#
browser support

all browsers supported by RadControls


PROJECT DESCRIPTION
I ran into the problem where I needed a column to be read-only after data was entered.  In this case, the Zip Code could not change once it was entered by the user.

        private void rgLocations_CellBeginEdit(object sender, GridViewCellCancelEventArgs e) 
        { 
            if (((GridViewDataColumn)this.rgLocations.CurrentColumn).UniqueName == "Zip Code" && 
                                     this.rgLocations.CurrentRow.Cells["Zip Code"].Value != DBNull.Value) 
            { 
                e.Cancel = true
                RadMessageBox.SetThemeName(this.ThemeName); 
                RadMessageBox.Show("You cannot edit zip codes of an existing Location"); 
            } 
        }  

2 Answers, 1 is accepted

Sort by
0
Svett
Telerik team
answered on 21 Apr 2010, 02:27 PM
<admin>question moved from code library to Forums</admin>

Hello Chad,

Your approach is right. You need to subscribe for CellBeginEdit event, where you can handle the desired behavior. If the current row is new row you have to allow editing of its columns. You need to modify your code as it shown in the following code snippet:

private void rgLocations_CellBeginEdit(object sender, GridViewCellCancelEventArgs e)
{
   if (this.radGridView.CurrentRow is GridViewNewRowInfo)
   {
       return;
   }
 
   if (((GridViewDataColumn)this.rgLocations.CurrentColumn).UniqueName == "Zip Code" &&
                                     this.rgLocations.CurrentRow.Cells["Zip Code"].Value != DBNull.Value)
   {
         e.Cancel = true;
         RadMessageBox.SetThemeName(this.ThemeName);
         RadMessageBox.Show("You cannot edit zip codes of an existing Location");
   }



Best wishes,
Svett
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
0
Chad Hensley
Top achievements
Rank 1
answered on 21 Apr 2010, 02:30 PM
This wasn't a question.  I was submitting a code sample for others. My code works, but your addition may have improved it.
Tags
GridView
Asked by
Chad Hensley
Top achievements
Rank 1
Answers by
Svett
Telerik team
Chad Hensley
Top achievements
Rank 1
Share this question
or