ASP.NET Gridview: Making a GridColumn noneditable in Edit mode

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
ID=”GridView2″
runat=”server”
AutoGenerateColumns=”False”
>


<Columns>


<asp:CommandField
ShowEditButton=”True”
/>


<asp:BoundField
DataField=”MyProperty”
HeaderText=”Int”
/>


<asp:BoundField
DataField=”MyProperty1″
HeaderText=”String”
/>


</Columns>


</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
void DataBindGrid()

{


List<Class1> lst = new
List<Class1>();


for (int i = 0; i < 10; i++)

{

lst.Add(new
Class1()

{

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
void GridView2_RowDataBound(object sender, GridViewRowEventArgs e)

{

BoundField bound = GridView2.Columns[1] as
BoundField;

      bound.InsertVisible = false;

      bound.ReadOnly = true;

 
 

 }

OR

 

ASPX

 

<asp:BoundField
DataField=”MyProperty”
HeaderText=”Int”
InsertVisible=”False”
ReadOnly=”True”
/>

 

 

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

 

 

 

Advertisements

3 Responses to “ASP.NET Gridview: Making a GridColumn noneditable in Edit mode”

  1. bigman Says:

    nice tuto in here , good introduction , look at mine in my website righ here http://help.uk.cm.

    hear from you soon thanks

  2. kumar s Says:

    hi Baimey,
    your tips made my task very simple, thank you so much.. 🙂


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: