Introduction
I came across a very common requirement in my project and thought that will have to find share with all.
This
article explains how to make a column, say Primary Key, in a gridview noneditable.
Create a Website
Create a new website File > New > Website |
Design (.aspx)
Select gridview from the toolbox |
|
|
Enable edit and add the required bound and template field according to the requirement
<asp:GridView
|
Include three events for the gridview:
OnRowDataBound=”GridView2_RowDataBound”
OnRowEditing=”GridView2_RowEditing”
Code Behind (.cs)
Do the databinding from code behind
Being a sample here I have bonded a list of objects to the GridView (Can be done according to the requirement).
private {
{ lst.Add(new { MyProperty = i, MyProperty1 = “String” + i }); } GridView2.DataSource = lst; GridView2.DataBind(); } |
//Add the class objects to the list with two properties
//Binding to gridview |
RowBound Event
To make the column 1 readonly on editing add this
Code Behind |
protected { BoundField bound = GridView2.Columns[1] as bound.InsertVisible = false; bound.ReadOnly = true; } |
OR |
|
ASPX |
<asp:BoundField |
Editing Event in codebehind
Along with editing we’ll have to handle
onrowcancelingedit=”GridView2_RowCancelingEdit”
onrowupdating=”GridView2_RowUpdating” as well.
But here we’ll see about the editing event
protected
void GridView2_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView2.EditIndex = -1;
DataBindGrid();
}
Conclusion
Here we have seen how to make a column noneditab;e in edit mode of a Gridview.
Happy Coding J
Thanks
Baimey
March 6, 2010 at 3:50 pm
nice tuto in here , good introduction , look at mine in my website righ here http://help.uk.cm.
hear from you soon thanks
March 8, 2010 at 7:21 am
Thanks … 🙂
March 22, 2011 at 5:35 am
hi Baimey,
your tips made my task very simple, thank you so much.. 🙂