DataGrid:Maintaining an Empty Row for the Entry of New Records
2012-06-17 18:42
309 查看
The final (and generally most user-friendly) method is to automatically maintain an empty row as the
last row in the DataGrid. The user can enter data for a new item in this row, and the DataGrid should
automatically add a new empty row once they do. Sadly, there is no built-in feature like this in the core
Silverlight DataGrid. You could 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 particular version of the
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 by
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 a
DomainDataSource control, you would do this in the LoadedData event of the
DomainDataSource control, like so:
DomainDataSourceView view = productDataGrid.ItemsSource as DomainDataSourceView;
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 the
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 == DataGridEditAction.Commit)
{
if (e.Row.DataContext == addRowBoundItem)
{
DomainDataSourceView view =
productDataGrid.ItemsSource as DomainDataSourceView;
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 the
new row):
DomainDataSourceView view = productDataGrid.ItemsSource as DomainDataSourceView;
view.Remove(addRowBoundItem);
last row in the DataGrid. The user can enter data for a new item in this row, and the DataGrid should
automatically add a new empty row once they do. Sadly, there is no built-in feature like this in the core
Silverlight DataGrid. You could 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 particular version of the
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 by
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 a
DomainDataSource control, you would do this in the LoadedData event of the
DomainDataSource control, like so:
DomainDataSourceView view = productDataGrid.ItemsSource as DomainDataSourceView;
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 the
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 == DataGridEditAction.Commit)
{
if (e.Row.DataContext == addRowBoundItem)
{
DomainDataSourceView view =
productDataGrid.ItemsSource as DomainDataSourceView;
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 the
new row):
DomainDataSourceView view = productDataGrid.ItemsSource as DomainDataSourceView;
view.Remove(addRowBoundItem);
相关文章推荐
- The Laws of Software Process: A New Model for the Production and Management of Software
- Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag
- Must qualify the allocation with an enclosing instance of type AAA (e.g. x.new A() where x
- 警告: Scene is unreachable due to lack of entry points and does not have an identifier for runtime acc
- Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag
- Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK fla
- The bundle does not contain an app icon for iPhone / iPod Touch of exactly '120x120' pixels
- Given an array nums, write a function to move all 0's to the end of it while maintaining the relativ
- Error 47 "xxxxxxx"is an invalid value for the "InputManifest" parameter of the "GenerateApplicationM
- android.util.AndroidRuntimeException Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want? com.uethinking.microvideo.manag
- Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK fla
- an out of memory error has occurred.Consult the 'running eclipse' section of the read me file for...
- 读《The End of an Architectural Era (It’s Time for a Complete Rewrite)》
- Why is the size of an empty class not zero in C++?
- Scene is unreachable due to lack of entry points and does not have an identifier for runtime access
- missing required icon file. the bundle does not contain an app icon for iphone/ipod touch of exactly
- The article discusses a couple of new features introduced for assemblies and versioning in Visual Studio 2005.
- Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag
- Scene is unreachable due to lack of entry points and does not have an identifier for runtime access
- An error occurred during local report processing.Failed to load expression host assembly. Details: Request for the permission of