WPF DataGrid e.Row.Item in DataGridCellEditEndingEventArgs from the CellEditEnding event is null when casted to DataRowView.
-
Wednesday, October 28, 2009 5:30 PM
I am trying to follow this is the example to commit changes with the WPF DataGrid on a cell by cell basis: http://www.scottlogic.co.uk/blog/wpf/2009/01/wpf-datagrid-committing-changes-cell-by-cell/
The problem is that when I try it with my code, e.Row.Item in DataGridCellEditEndingEventArgs from the CellEditEnding event is null when I try to cast to DataRowView. The difference between my code and the example is that I am binding with LINQ2SQL like this:
var result = from t in data.Topics
select new { t };
dataGrid.ItemsSource = result;
This the XAML for the DataGrid:
<dg:DataGrid x:Name="dataGrid" AutoGenerateColumns="False" CellEditEnding="DataGrid_CellEditEnding" CurrentCellChanged="DataGrid_CurrentCellChanged">
<dg:DataGrid.Columns>
<dg:DataGridTextColumn Header="Topic" Width="150" Binding="{Binding Path=t.TopicTitle}"/>
</dg:DataGrid.Columns>
</dg:DataGrid>
These are the event handlers:
private void DataGrid_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e)
{
_dataRow = e.Row.Item as DataRowView;
}private void DataGrid_CurrentCellChanged(object sender, EventArgs e)
{
if (_dataRow != null)
{
_dataRow.EndEdit();
data.SubmitChanges();
_dataRow = null;
}
}
I am not sure what or where I have done something wrong. Any help would be appreciated.
All Replies
-
Wednesday, November 04, 2009 4:59 PM
Hi,
I think the items in the DataGrid are not of DataRowView type, you can set use the following code to check whether the item you get is null, if it's not null, check its type.
private void DataGrid_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e)
{
if (e.Row.Item != null)
{
// Check the item type
Console.WriteLine(e.Row.Item.GetType().Name);
// If the item is for example of MyClass type, you can do the casting as follows:
// MyClass itemdata = e.Row.Item as MyClass;
}
}
If anything is unclear, please feel free to let me know.
Best Regards,
Zhi-Xin Ye
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Welcome to the All-In-One Code Framework!- Marked As Answer by Zhi-Xin Ye Wednesday, November 11, 2009 7:54 AM
-
Tuesday, March 27, 2012 7:26 PM
Hi,
Friend, I just need to edit a datagrid, which already seeks data bank. I wish onlyafter seeing the datagrid loaded with data, can enter new values and press Enter or Tab after ... data to be recorded in the bank.
I think the items in the DataGrid are not of DataRowView type, you can set use the following code to check whether the item you get is null, if it's not null, check its type.
private void DataGrid_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e)
{
if (e.Row.Item != null)
{
// Check the item type
Console.WriteLine(e.Row.Item.GetType().Name);
// If the item is for example of MyClass type, you can do the casting as follows:
// MyClass itemdata = e.Row.Item as MyClass;
}
}
If anything is unclear, please feel free to let me know.
Best Regards,
Zhi-Xin Ye
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Welcome to the All-In-One Code Framework!
Well, I only got so far to edit the data, but only recovered the text that was alreadythe current text can not.

