WPF DataGrid Cell update

Dec 28, 2010 at 3:58 PM

hello,

I have a wpf datagrid that i need to update a column based on two other column's values.  So the idea is three columns, Start Time, End Time, Total Time.  I am trying to update the Total Time based off the start time and end time cell contents.  For now, i have tried to use the CellEditEnding event and set the FrameworkElement value for the Total Time.  This works but does not update the datasource.

 if (e.Column.Header.ToString().Contains( "Start Time:") || e.Column.Header.ToString().Contains("End Time:"))
            {
                try
                {
                    if (!isEdit)
                    {
                        isEdit = true;
                        DateTime endTime = new DateTime();
                        DateTime startTime = new DateTime();
                        FrameworkElement elapsed = groupGrid.Columns[5].GetCellContent(e.Row);
                        FrameworkElement end = groupGrid.Columns[4].GetCellContent(e.Row);
                        FrameworkElement start = groupGrid.Columns[3].GetCellContent(e.Row);
                        if (end.GetType().ToString().Contains("TextBlock"))
                            endTime = Convert.ToDateTime(((TextBlock)end).Text);
                        else if (end.GetType().ToString().Contains("TextBox"))
                            endTime = Convert.ToDateTime(((TextBox)end).Text);
                        if (start.GetType().ToString().Contains("TextBlock"))
                            startTime = Convert.ToDateTime(((TextBlock)start).Text);
                        else if (start.GetType().ToString().Contains("TextBox"))
                            startTime = Convert.ToDateTime(((TextBox)start).Text);
                       ((TextBlock)elapsed).Text = endTime.TimeOfDay.Subtract(startTime.TimeOfDay).ToString();
                        elapsed.Focus();
                        if (elapsed.IsFocused)
                        {
                            FrameworkElement parent = (FrameworkElement)elapsed.Parent;
                            parent.Focus();
                        }
                        DataGrid grid = (DataGrid)sender;
                        grid.CommitEdit(DataGridEditingUnit.Row, true);
                        isEdit = false;
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }