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

Prevent editing of primary keys

7 Answers 111 Views
GridView
This is a migrated thread and some comments may be shown as answers.
James
Top achievements
Rank 1
James asked on 17 Jul 2009, 12:46 PM
What is the best way to prevent editing of primary key column(s) in the data grid?  I do not want the user to edit existing primary key column(s), however, I do want them to be able to edit the primary key column(s) when adding a new row.

Thanks for your help in advance,

James

7 Answers, 1 is accepted

Sort by
0
James
Top achievements
Rank 1
answered on 17 Jul 2009, 03:39 PM
I forgot to mention that I am using natural primary keys vs surrogate keys (auto generated), therefore, I can not just hide  the primary keys in the grid and then auto generate the keys. 
0
Jack
Telerik team
answered on 20 Jul 2009, 06:58 AM
Hi James,

Maybe the best option in this case is to handle the CellBeginEdit event. Just set the e.Cancel property to true. Here is a sample:

void radGridView1_CellBeginEdit(object sender, GridViewCellCancelEventArgs e) 
    if (((GridViewDataColumn)this.radGridView1.CurrentColumn).FieldName == "ID" 
        && this.radGridView1.CurrentRow is GridViewDataRowInfo) 
    { 
        e.Cancel = true
    } 

If you need further assistance, please write us back.

Best wishes,
Jack
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
0
James
Top achievements
Rank 1
answered on 21 Jul 2009, 06:04 PM
Jack,

This would cancel the edit even if you are entering data on a new row for the ID column.  I need the user to be able to enter a value for the ID column on a new row, and not allow edits on the ID column for an existing row.

Thanks,

James
0
Jack
Telerik team
answered on 22 Jul 2009, 09:00 AM
Hello James,

This code will work with our latest release. I didn't noticed that you are using an older version. I recommend that you try our latest release. It contains many new features and improvements and we will appreciate your feedback.

You should change a little the condition when using a previous version of RadControls. Here is the modified code:

void radGridView1_CellBeginEdit(object sender, GridViewCellCancelEventArgs e) 
    if (((GridViewDataColumn)this.radGridView1.CurrentColumn).FieldName == "ID" 
        && !(this.radGridView1.CurrentRow is GridViewNewRowInfo)) 
    { 
        e.Cancel = true
    }  

I hope this helps. Should you have any other questions, feel free to ask.

Regards,
Jack
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
0
James
Top achievements
Rank 1
answered on 22 Jul 2009, 09:46 PM
Jack,

That worked great, thanks for your help.  I converted the C# code to VB using Telerik's code converter (by the way another fine piece of work from Telerik).  Just click the link at the bottom of the Community page on Telerik's web site to run the code converter.

Sub radGridView1_CellBeginEdit(sender As Object, e As   
GridViewCellCancelEventArgs)<BR>&nbsp;If   
(DirectCast(Me.radGridView1.CurrentColumn, GridViewDataColumn)).FieldName = "ID"   
AndAlso Not (TypeOf Me.radGridView1.CurrentRow Is GridViewNewRowInfo)   
Then<BR>&nbsp;&nbsp;e.Cancel = True<BR>&nbsp;End If<BR>End Sub 

Thanks very much for your help,

James
0
James
Top achievements
Rank 1
answered on 22 Jul 2009, 09:50 PM
Sorry about the code formatting, I obviously do not know how to use the code formatting tool.

James
0
Jack
Telerik team
answered on 23 Jul 2009, 08:30 AM
James, I am glad to hear that the solution worked for you. If you have any other questions, I will be glad to help.

Kind regards,
Jack
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
Tags
GridView
Asked by
James
Top achievements
Rank 1
Answers by
James
Top achievements
Rank 1
Jack
Telerik team
Share this question
or