DataGrid: maintaining an empty row for the entry of new records

Source: Internet
Author: User

The final (and generally most user-friendly) method is to automatically maintain an empty row as
Last row in the DataGrid. the user can enter data for a new item in this row, and the DataGrid shocould
Automatically Add a new empty row once they do. Sadly, there is no built-in feature like this in the core
Silverlight DataGrid. You cocould add it yourself (all the source code for the DataGrid Control is available in
The Silverlight Toolkit), but it's not a great idea, as you 'd be tying yourself to that particle version of
DataGrid Control, and wouldn't be able to (easily) take advantage of new features and bug fixes in future
Releases of Silverlight and the Silverlight toolkit. You can, however, handle a number of events raised
The DataGrid control and manage this automatically. The steps are as follows:
1. Maintain a class-level variable that will reference the new item object:
Private object addrowbounditem = NULL;
2. Add a new item to the bound collection before (or shortly after) binding, and
Assign this item to the class-level variable. If binding the DataGrid directly to
Domaindatasource control, you wocould do this in the loadeddata event of
Domaindatasource control, like so:

Domainperformanceview view = productdatagrid. itemssource as domainperformanceview;
Addrowbounditem = new product ();
View. Add (addrowbounditem );
3. Handle the roweditended event of the DataGrid control. If the row being
Committed is the empty row item that was edited (you can get the item in
Collection that it is bound to from the datacontext property of the row), then
It's time to add a new item to the end of the bound collection, ensure it is
Visible, select it, and put it in edit mode. For example:

Private void productdatagrid_roweditended (Object sender,
Datagridroweditendedeventargs E)
{
If (E. editaction = maid action. Commit)
{
If (E. Row. datacontext = addrowbounditem)
{
Domainperformanceview view =
Productdatagrid. itemssource as domainperformanceview;
Addrowbounditem = new product ();
View. Add (addrowbounditem );
Productdatagrid. selecteditem = addrowbounditem;

Productdatagrid. currentcolumn = productdatagrid. Columns [0];
Productdatagrid. scrollintoview (addrowbounditem,
Productdatagrid. currentcolumn );
Productdatagrid. beginedit ();
}
}
}

4. Remember to always Delete the last item in the collection before submitting
The changes back to the server (as it will always be the item representing
New row ):
Domainperformanceview view = productdatagrid. itemssource as domainperformanceview;
View. Remove (addrowbounditem );

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.