Scott on Writing

Musings on technical writing...

Creating a Fully Editable DataGrid Server Control - Comments Appreciated!

The DataGrid provides row-by-row editing capabilities via the following players:

  • The EditCommandColumn DataGridColumn, which renders a column of Edit/Update/Cancel buttons in the DataGrid.
  • The EditItemIndex property, which indicates the index of the row that is editable, and
  • The DataGrid databinding process, which enumerates the DataSource and creates a DataGridItem for each record
  • The EditCommand/UpdateCommand/CancelCommand events, which fire when the Edit/Update/Cancel buttons are clicked.  These events are useful because a page developer will create an event handler and perform the steps necessary to provide editing capabilities (such as setting the EditItemIndex property accordingly, issuing an UPDATE statement to the database, and so on....)

These four actors make editing database data on a row-by-row basis a relatively simple task, much much much simpler than was possible in classic ASP. Some situations, though, require a fully editable DataGrid, one where all rows are editable at once, rather than on a row-by-row basis. In such scenarios, the end user wants to be able to make any number of changes and then click an "Update All" button.

The DataGrid does not provide this capability inherently. In my book, ASP.NET Data Web Controls Kick Start, I examine how to use a standard DataGrid to provide such functionality, namely through making each editable column in the DataGrid a TemplateColumn where the TemplateColumn's ItemTemplate has the editing interface (such as a TextBox Web control, or DropDownList, or whatever Web control(s) is/are suitable for the editing interface). Additionally, a "Save All" button is included that, when clicked, iterates through the DataGrid's Items collection and issues an UPDATE statement for each DataGrid row. While this approach clearly works, it is a departure from the DataGrid's existing functionality for doing updates.

With these thoughts, I set out to create a fully editable custom control derived from the DataGrid class. My vision was to have a control that provided full editing capabilities via:

  • Some means to display "Edit All Rows"/"Update All Rows"/"Cancel Batch Update" buttons, similar to the Edit/Update/Cancel buttons in each row for the row-by-row editable DataGrid. Rather than having these buttons appear in each row, though, I'd just want them to appear once, like maybe above the DataGrid.
  • A BatchUpdate property - if true, all rows would be displayed as editable, if false, none would. Therefore, a page developer could toggle between all rows being editable or not by setting this property and rebinding the data to the grid.
  • EditBatchCommand/UpdateBatchCommand/CancelBatchCommand events, which the page developer could create event handlers for to take the necessary steps to update the database, or to toggle the BatchUpdate value.

Initially this seemed like an easy task. Created a class that derived from DataGrid, added a BatchUpdate property, overrode the DataGrid's CreateItem() method so that if an Item or AlternatingItem was being created and BatchUpdate was True, then the DataGridItem was created as an EditItem. Added the events, and then set out to add the buttons. And that's where I got stuck.

Initially I thought I'd just override the CreateChildControls() method and add buttons to the start of the control hierarchy, thereby having them appear above the DataGrid. No dice, as the DataGrid's PrepareControlHierarchy() method naively assumes that the first control in the hierarchy is the DataGrid's Table control. This approach worked, though, if I slapped the buttons at the end of the control hierarchy, but then the buttons would always appear at the bottom of the grid, and I wanted more flexibility.

An ideal approach, I reckoned, would to be to create a new DataGridItem type, like the Pager type, that would add a row to the top and/or bottom of the DataGrid with the buttons. However, this would involve a major reworking of the DataGrid, requiring the overriding of many methods and classes. Way more work than I wanted to do.

My end solution - and here's where I'm looking for comments - was to separate out the functionality into two server controls. An editable grid called EditGrid, and what I call an EditBar. The EditBar displays the "Edit All Rows"/"Update All Rows"/"Cancel Batch Update" buttons and has a property EditGridID, which must refernece the ID property of the EditGrid the EditBar works for. Then, when one of the EditBar's button is clicked, the appropriate EditGrid event is raised. Separating the EditGrid and EditBar has the advantage that the EditBar can be placed anywhere on the page and operate on the EditGrid, but it feels "messy" to me to have a control reference another control. I know the validation controls use a technique not unlike this with their ControlToValidate property, but I was wondering if anyone could think of a better way.

Any comments/ideas/suggestions most appreciated. For the record, I am working on an article on this topic, so any ideas or improvements suggested that are utilized will be given full credit in the article, naturally. Thanks!

posted on Thursday, January 08, 2004 10:47 AM

Feedback

# Re: Creating a Fully Editable DataGrid Server Control - Comments Appreciated! 1/8/2004 1:05 PM aspZone.com

# re: Creating a Fully Editable DataGrid Server Control - Comments Appreciated! 1/8/2004 11:53 PM Justin Lovell

I was thinking that you should go on something along the lines of what your initially thought. It is something that I like to call "Better late than never". You go about this by allowing the DataGrid to do its processing, then you would then override the OnPreRender method. You will allow the actual DataGrid to do its PreRender event processing and then slap the button controls at the top of the DataGrid.

protected override void OnPreRender(EventArgs e) {
base.OnPreRender(e);
// slap the controls at the top of the DataGrid... example:
Controls.AddAt(0, new Button());
}

# re: Creating a Fully Editable DataGrid Server Control - Comments Appreciated! 1/9/2004 8:56 AM Scott Mitchell

Justin, your approach won't work. The DataGrid's PrepareControlHierarchy() method is called from the Render() method. :-/

I tried overloading the Render() method, calling PrepareControlHierarchy(), THEN manually inserting the controls at the beginning of the hierarchy. There were state management problems - like the click event not bubbling up - since the control isn't present in the LoadViewState and RaisePostBackEvent stages of the page's lifecycle. :-(

# DataGrid Dilema 1/10/2004 1:14 AM Justin Lovell's Blog

# DataGrid Dilema 1/10/2004 1:20 AM Justin Lovell's Blog

# re: Creating a Fully Editable DataGrid Server Control - Comments Appreciated! 1/10/2004 1:27 AM Justin Lovell

Oops - sorry for the double comment.

# re: Creating a Fully Editable DataGrid Server Control - Comments Appreciated! 11/12/2004 7:20 AM Sridhar

Hi,

I was looking for a datagrid which you have mentioned above for an application. can you please let me know whether it would be possible to do and if you have done it, please let me know how to do it. My email address is dsridhar18@yahoo.com

Thanks,
Sridhar!!

# re: Creating a Fully Editable DataGrid Server Control - Comments Appreciated! 2/3/2005 2:19 PM Murray Roke

I'll pose a scenario for you to think about.
Which may raise other features you may wish to implement.

If you think of an 'order' you often want a list of products with the quantity column editable for all rows, but no other columns.
Also each row should have a delete button.
Then you have some other fields (say for shipping) then at the bototm you have a submit button that updates _everything_ including the line items and the datagrid.

let me know if you make something I can use :)
murray (@t) terabyte . co . nz

# re: Creating a Fully Editable DataGrid Server Control - Comments Appreciated! 5/17/2005 6:52 AM ken

Did not find link to download of source code!

# re: Creating a Fully Editable DataGrid Server Control - Comments Appreciated! 5/20/2005 8:33 AM mark

Did you get anywhere with this? Or did you give up on it?

Title:  
Name:  
Url:
Protected by Clearscreen.SharpHIPEnter the code you see:
Comments   

Add To Your Reader

My Links

Archives

Post Categories

 

I am a Microsoft MVP for ASP.NET.
I am an ASPInsider.
<May 2008>
SMTWTFS
27282930123
45678910
11121314151617
18192021222324
25262728293031
1234567

Comment Stats

DayTotal% of Total
Sunday 1866.8%
Monday 37913.9%
Tuesday 45316.7%
Wednesday 50418.5%
Thursday 53519.7%
Friday 49418.2%
Saturday 1666.1%
Total 2717100.0%

Hour1Total% of Total
12:00 AM 652.4%
1:00 AM 682.5%
2:00 AM 622.3%
3:00 AM 742.7%
4:00 AM 572.1%
5:00 AM 1033.8%
6:00 AM 1084.0%
7:00 AM 1585.8%
8:00 AM 1716.3%
9:00 AM 1475.4%
10:00 AM 1716.3%
11:00 AM 1816.7%
12:00 PM 1886.9%
1:00 PM 1696.2%
2:00 PM 1605.9%
3:00 PM 1324.9%
4:00 PM 1073.9%
5:00 PM 923.4%
6:00 PM 913.3%
7:00 PM 963.5%
8:00 PM 833.1%
9:00 PM 782.9%
10:00 PM 792.9%
11:00 PM 772.8%
Total 2717100.0%

Comments by Blog Entry Date/Time

Day Entry MadeAvg.Total
Sunday 5.54144
Monday 5.22339
Tuesday 4.28419
Wednesday 7.67637
Thursday 6.90607
Friday 5.48411
Saturday 5.33160
Total 5.842717

Hour1 Entry MadeAvg.Total
12:00 AM 5.0035
1:00 AM 1.002
5:00 AM 0.000
7:00 AM 7.0035
8:00 AM 5.35107
9:00 AM 6.32278
10:00 AM 6.47246
11:00 AM 4.41181
12:00 PM 6.88330
1:00 PM 3.00111
2:00 PM 5.41222
3:00 PM 8.64285
4:00 PM 4.0589
5:00 PM 5.92154
6:00 PM 4.52113
7:00 PM 9.67174
8:00 PM 9.80147
9:00 PM 5.05111
10:00 PM 5.4265
11:00 PM 4.5732
Total 5.842717

Learn More About Comment Stats
1 - All times GMT -8...


Blog Stats

Favorite Web Sites

My Books

My MSDN Articles