Click here to Skip to main content
12,622,291 members (32,988 online)
Click here to Skip to main content
Add your own
alternative version


73 bookmarked

Custom editable Datagrid with paging and sorting

, 3 Dec 2003
Rate this:
Please Sign up or sign in to vote.
How to create a grid control for easy editing


This article shows you how to create a custom DataGrid with paging, alternate sorting, and is also completely editable


When I was developing some pages, I lost a lot of time with Datagrids. The code here is to made that work more simple. Only drag the control to the page, complete some properties, point to the methods that controls the updating, deleting and you have a complete editable grid.

[ToolboxData("<{0}:editableElphGrid runat="\""server\">
//ParseChildren allow to work with templates inside the custom datagrid
public class editableElphGrid : System.Web.UI.WebControls.DataGrid
    /// <span class="code-SummaryComment"><summary>

Well, now add the control on a page, and then you must fill the fields .

DataKeyField: That is the key field of the table, the unique identifier of a row. I put because I use that on Database actions, like delete a specific row etc.

And on the tab of Events :- GetData, CancelData, EditData and DeleteData.

Finally you should have something like this.

On the aspx page:

<cc1:editableElphGrid id="EditableElphGrid1" runat="server" 

On the code behind

protected elphControls.editableElphGrid EditableElphGrid1;
private void InitializeComponent()
 this.EditableElphGrid1.DeleteData += 
   new elphControls.editableElphGrid.eliminarDatos(this.del);
 this.EditableElphGrid1.GetData += 
   new elphControls.editableElphGrid.obtenerDatos(this.obt);
this.EditableElphGrid1.EditData += 
   new elphControls.editableElphGrid.editarDatos(this.edit);
 this.EditableElphGrid1.UpdateData += 
   new elphControls.editableElphGrid.actualizarDatos(this.act);
 this.EditableElphGrid1.CancelData += 
   new elphControls.editableElphGrid.cancelarDatos(this.cncl);
 this.Load += new System.EventHandler(this.Page_Load);
private System.Data.DataTable obt()
OleDbConnection conn=new OleDbConnection(
    "Provider=Microsoft.Jet.OLEDB.4.0;Data source=E:\\bdProves.mdb");
 OleDbDataAdapter adapter=new OleDbDataAdapter("select * from tabla",conn);
 DataSet ds=new DataSet();
 return ds.Tables["aa"];
private void edit(int dataKey, 
    System.Web.UI.WebControls.DataGridCommandEventArgs e)
//here the logic asociated with edit comand... if you have
private void del(int dataKey, 
    System.Web.UI.WebControls.DataGridCommandEventArgs e)
 //here the logic asociated with delete comand
private void cncl(int dataKey, 
    System.Web.UI.WebControls.DataGridCommandEventArgs e)
 //here the logic asociated with Cancel comand
private void act(int dataKey, 
    System.Web.UI.WebControls.DataGridCommandEventArgs e)
 //here the logic asociated with update comand


Changed the session to save the sorting params for a viewstate, now you can have two grids on the same page. Also Changed the declaration of the first sorting field, now this isn't necessary. Thanks to Zanoza.


I think that this grid is a good base to customize a datagrid. Hope it could be useful. All comments about this are welcomed.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

You may also be interested in...


Comments and Discussions

Questionevents firing twice Pin
mojkurs9-Feb-06 16:39
membermojkurs9-Feb-06 16:39 
AnswerRe: events firing twice Pin
mojkurs9-Feb-06 17:54
membermojkurs9-Feb-06 17:54 
GeneralGetting text from edit/update Pin
bkerr1-Nov-05 20:25
memberbkerr1-Nov-05 20:25 
GeneralDataGrid : column width Pin
thanchu2-Oct-05 19:22
memberthanchu2-Oct-05 19:22 
GeneralProgramming Style Tips Pin
Pinhead_Me18-Oct-04 10:36
memberPinhead_Me18-Oct-04 10:36 
When naming your objects, its best to use a coding style that provides additional context to your application logic and structure.


for a DataGrid
DataGrid dgProducts; //Note that just by looking at that object one can easily deduce it is a Datagrid.

Also, events that are associated with that DataGrid can be coded like this:


also, using del, cncl, act method names adds little context, it would be best to spell out method names for clarity. Leaving out author names in objects is also recommended and rarely, if ever, practiced by non-egotistical programmers.

GeneralRe: Programming Style Tips Pin
Klom Dark23-Jan-06 5:43
memberKlom Dark23-Jan-06 5:43 
GeneralRe: Programming Style Tips Pin
Pinhead_Me23-Jan-06 8:39
memberPinhead_Me23-Jan-06 8:39 
GeneralRe: Programming Style Tips Pin
mtone28-Jan-07 5:32
membermtone28-Jan-07 5:32 
Generallogic asociated with update comand Pin
Hamish Ahern18-Oct-04 2:31
memberHamish Ahern18-Oct-04 2:31 
QuestionCan you write VB.NET? Pin
Hamish Ahern18-Oct-04 1:40
memberHamish Ahern18-Oct-04 1:40 
GeneralHola Pin
Fernando Finelli21-Jul-04 8:04
memberFernando Finelli21-Jul-04 8:04 
GeneralControl LoadViewState Pin
Rruedi3-Jul-04 7:11
memberRruedi3-Jul-04 7:11 
General@ Register Pin
fsdkjfskl17-Mar-04 5:02
memberfsdkjfskl17-Mar-04 5:02 
QuestionHow to use the source file? Pin
deny0123-Jan-04 4:46
memberdeny0123-Jan-04 4:46 
QuestionHow do I had non autogenerate columns ? Pin
Ernest Bariq25-Dec-03 23:20
memberErnest Bariq25-Dec-03 23:20 
AnswerRe: How do I had non autogenerate columns ? Pin
Ernest Bariq30-Dec-03 6:51
memberErnest Bariq30-Dec-03 6:51 
GeneralError Pin
GeorgeMar10-Dec-03 6:31
memberGeorgeMar10-Dec-03 6:31 
GeneralRe: Error Pin
Elph10-Dec-03 11:33
memberElph10-Dec-03 11:33 
GeneralRe: Error Pin
GeorgeMar11-Dec-03 0:14
memberGeorgeMar11-Dec-03 0:14 
GeneralRe: Error Pin
Anonymous11-Dec-03 2:26
sussAnonymous11-Dec-03 2:26 
GeneralRe: Error Pin
GeorgeMar11-Dec-03 4:05
memberGeorgeMar11-Dec-03 4:05 
GeneralProblem with first loading Pin
Zanoza5-Dec-03 5:27
memberZanoza5-Dec-03 5:27 
GeneralRe: Problem with first loading Pin
Elph5-Dec-03 8:56
memberElph5-Dec-03 8:56 
GeneralThow! Spanish! Pin
Jonathan de Halleux4-Dec-03 21:15
memberJonathan de Halleux4-Dec-03 21:15 
GeneralRe: Thow! Spanish! Pin
Elph4-Dec-03 21:33
memberElph4-Dec-03 21:33 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.161128.1 | Last Updated 4 Dec 2003
Article Copyright 2003 by Elph
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid