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

Some silverlight tutorials

1. First Look at Silverlight 2 (http://weblogs.asp.net/scottgu/archive/2008/02/22/first-look-at-silverlight-2.aspx)

In this article, you will have a first look at Silverlight that focuses on enabling Rich Internet Application (RIA) development.

2. The Importance of an Impressive User Interface (http://www.learn-silverlight-tutorial.com/AnimatingSilverlight.cfm#h1.1)

In this lesson of the Silverlight tutorial, you will learn about the elements used for drawing graphics and to animate graphics by using Expression Blend.

3.How to Start with Silverlight (http://www.dotnetfunda.com/tutorials/silverlight/)

4.Creating a XAML project for the Web (http://www.scriptol.com/silverlight/web-project.php)

They are beginning this tutorial by the creation of a project for a Web application, running in a navigator, and who requires the Silverlight plug-in, plus the SDK for programmers.

5.      Silverlight Tutorials Collection (http://www.wynapse.com/Silverlight_Tutorials.aspx)

A good and useful collection of Silverlight tutorials if you want to learn how to get start with it?

6.How To Create An Animated Navigation Bar (http://www.c-sharpcorner.com/UploadFile/mdobbles/SilverlightNavBar07272008212846PM/SilverlightNavBar.aspx)

This article is a tutorial on how to use Silverlight 2.0 to make a nifty animated navigation bar much like the one at the top of the Silverlight.net website.

7.Virtual Earth Silverlight Minimap Tutorial (http://www.earthware.co.uk/blog/index.php/2009/03/virtual-earth-silverlight-minimap-tutorial/)

In this tutorial author is going to show you how to create a simple styled minimap to use in your Virtual Earth Silverlight projects that looks exactly like the one we used in our Twittermap demo.

8.Silverlight 3 Photo Gallery Wall Application (http://designwithsilverlight.com/2009/03/31/silverlight-3-photo-gallery-wall-application/)

A downloadable source code of Silverlight 3 Photo Gallery Wall Application with useful information to understand how it was done.

9.Silverlight With Java Script Tutorial (http://www.wynapse.com/Silverlight/Tutor/Silverlight_With_Java_Script_Tutorial_07.aspx)

This tutorial will introduce interactive animation. With Interactive Animation, we have the ability to start and stop our animations when we want without having to resort to subterfuge.

10.  Creating Rich Data Forms in Silverlight 3 – Introduction (http://www.silverlightshow.net/items/Creating-Rich-Data-Forms-in-Silverlight-3-Introduction.aspx)

In this article author will show you the basics of the DataForm control – what you can do with it and why you should use it. He will show you how you can bind an item or a collection of items to a data form. Microsoft guys have done a great job with this control, so there are many features that you can use directly without much pain.

11.  Securing Silverlight Application and WCF Service using ASP.Net Authentication Techniques (http://smehrozalam.wordpress.com/2009/01/07/securing-silverlight-application-and-wcf-service-using-aspnet-authentication-techniques/)

In this article you can learn the securing Silverlight Application and WCF Service using ASP.Net Authentication Techniques.

12.  Animation Easings in Silverlight 3 (http://www.silverlightshow.net/items/Animation-Easing-in-Silverlight-3.aspx)

In this author will describe in details how you can use the BackEase animation.

13.  The Silverlight BlockText Control (http://www.silverlightshow.net/items/The-Silverlight-BlockText-Control.aspx)

The main idea is to let Silverlight’s Grid do the work. So at the beginning Author split the given string into words and draw them hidden into temporary grid, so he get the ActualWidth of each word.

14.  Introduction to XAML Browser Applications (http://aspalliance.com/1824_Introduction_to_XAML_Browser_Applications_XBAP.1)

This article gives an introduction to XBAPs and provides information about the differences between WPF Standalone applications, Silverlight, and XBAPs. Nihar explains the steps used to create a sample XBAP application and how to deploy and run it.

15.  50 Silverlight Applications (http://flashenabledblog.com/2007/07/09/from-a-to-z-50-silverlight-applications/)

This is the collection of 50 sample applications developed in Silverlight.

Hello world!

Welcome to WordPress.com. This is your first post. Edit or delete it and start blogging!