Gridview insert delete with custom objects in viewstate
I want to design a nested gridview with insert, update, delete functionality through custom business objects dynamically inside viewstate.
(Master Gridview)
EmployeeID EmployeeName
1 Ted
(Child Gridview)
ItemID ItemName
1 Keyboard
2 Mouse
2 John
(Child Gridview)
ItemID ItemName
1 PSU
开发者_如何学编程 2 GPU
3 Printer
I have done this in the past with datatable/dataviews but they are a real memory hog.
How do I implement Master/Detail functionality with business objects ?
What is the alternate to Dataview in master detail/business objects.
I know its a rather broad question but its worth getting started on something =)
Honestly, GridViews aren't going to provide you with huge performance gains over the other table-related ASP.NET controls. If you write a hierarchical object as follows:
public class Employee
{
[Constructors go here]
public int EmployeeID { get; private set; }
public string EmployeeName { get; private set; }
public List<Child> Children { get; private set; } // Or Collection, Array, etc.
}
You're still going to have to listen for the RowDataBound
event, cast your row's DataItem
to your child class, and then programatically assign its properties to the GridViewRow.
On top of that, you'll have the pleasure of creating a TemplateField
in which you close the current <td>
and <tr>
tags (or a <div>
or <span>
tag, if you're feeling marginally less masochistic), and then embed another GridView inside the table your GridView
has already generated.
Note that we haven't gotten to the point where you insert, delete, and update.
Honestly, if you have the time, try using AJAX (the real stuff, not the AutoPostBack
junk). ASP.NET WebMethods can return HTML from ASP.NET controls by calling .RenderControl()
, and unless you absolutely need to load the entire grid and all its children in one fell swoop and display all of it to your user, it'll more than likely get you a shorter page load time, and decrease the load on your webserver.
If you combine this with jQuery, calling the ASP.NET WebMethods is easy. As someone who's done this both ways, I'd argue that the jQuery/AJAX method is actually easier than writing some honking LINQ statement (or ADO.NET query), building a gigantic business object, and trying to manage all of it in one page load.
On the other hand, if you're bound and determined to use ASP.NET for this, I'd highly suggest that you read through the MSDN articles on the RowEditing event. It details some of the finer points to correctly setting and getting the EditIndex, and breaks down its events into tiny functions, which are an absolute necessity for something like this. In addition to what you see there, I'd suggest that you store your collection of business objects in the session and modify them on any row edit/create/delete event, then re-bind your GridView.
Using this method, your users would ideally have to click a submit button to actually save any changes. This allows you (and them) to avoid a lot heartache when someone accidentally deletes an item, or updates it with an invalid value.
Not 100% sure of the question, but it looks like the first thing you are asking is how do I define a master/detail relationship within a business object. In your example of an employee with children you could have an object like:
public class Employee
{
public int EmployeeId {get;set;}
public string EmployeeName {get;set;}
public List<Child> Children {get;set;}
}
Then when it comes to binding to your master grid view at each level you would just bind to the collection that you want. So level 1 would be bound to a List<Employee>
, and then level 2 would be bound to Employee.Children which would be a List<Child>
. You would most likely want to bind level 2 during the RowDataBound event of the master grid view
The alternative to dataview for a complex object like a business object is really whatever you make it. If you are wanting to only manage the inserting, updating, deleting in viewstate then you are also probably going to want to have your own custom means of doing this as the out of the box data sources may not work the way you want them to.
精彩评论