/// <summary>
        /// Called when the UnBoundRow cell gets end edited.
        /// </summary>
        /// <param name="dc">DataColumnBase Which holds GridColumn, RowColumnIndex and GridCell</param>
        /// <param name="record">DataContext of the row</param>
        /// <param name="canResetBinding">Reset Binding the CurrentCell</param>
        /// <returns></returns>
        public override bool EndEdit(DataColumnBase dc, object record, bool canResetBinding = false)
        {
            var cellRowColumnIndex = new RowColumnIndex(dc.RowIndex, dc.ColumnIndex);
            var cellElement        = dc.ColumnElement;
            var column             = dc.GridColumn;

            if (!this.HasCurrentCellState)
            {
                return(false);
            }
            if (!this.IsInEditing)
            {
                return(false);
            }
            if (this.SupportsRenderOptimization)
            {
#if WPF
                E uiElement = null;
                if (!UseOnlyRendererElement && cellElement is GridCell)
                {
                    uiElement = (E)((cellElement as GridCell).Content is FrameworkElement ? (cellElement as GridCell).Content as FrameworkElement : cellElement);
                }
                else
                {
                    uiElement = cellElement as E;
                }
                uiElement.PreviewLostKeyboardFocus -= OnLostKeyboardFocus;
#endif
                //this.IsFocused = false;
                this.SetFocus(false);

                GridUnBoundRowEventsArgs gridUnBoundRowEventsArgs = (cellElement as GridCell).ColumnBase.GridUnBoundRowEventsArgs;
                var dataColumn = (cellElement as GridCell).ColumnBase;

                OnEditingComplete(dataColumn, CurrentCellRendererElement);
                if (!UseOnlyRendererElement && cellElement == null)
                {
                    throw new Exception("Cell Element will not be get null for any case");
                }
                OnUnloadUIElements(dataColumn);

                OnPrepareUIElements(dataColumn, record, false);

                if (gridUnBoundRowEventsArgs != null && !gridUnBoundRowEventsArgs.hasCellTemplate)
                {
                    UpdateCurrentCellState((cellElement as GridCell).Content as FrameworkElement, false);
                }
                else
                {
                    UpdateCurrentCellState(cellElement as FrameworkElement, false);
                }
            }
            else
            {
                UpdateCurrentCellState(this.CurrentCellRendererElement, false);
            }

            return(!this.IsInEditing);
        }
        /// <summary>
        /// Called from <see>
        ///         <cref>IGridCellRenderer.PrepareUIElments</cref>
        ///     </see>
        ///     to
        /// prepare the UnBoundRow cells UIElement children.
        /// VirtualizingCellRendererBase overrides this method and
        /// creates new UIElements and wires them with the parent cells control.
        /// </summary>
        /// <param name="dataColumn">DataColumn Which holds GridColumn, RowColumnIndex and GridCell </param>
        /// <param name="record">record of the row </param>
        /// <param name="isInEdit"></param>
        /// <returns></returns>
        protected internal override FrameworkElement OnPrepareUIElements(DataColumnBase dataColumn, object record, bool isInEdit)
        {
            RowColumnIndex           cellRowColumnIndex       = new RowColumnIndex(dataColumn.RowIndex, dataColumn.ColumnIndex);
            FrameworkElement         cellContainer            = dataColumn.ColumnElement;
            FrameworkElement         cellcontent              = null;
            GridUnBoundRowEventsArgs gridUnBoundRowEventsArgs = dataColumn.GridUnBoundRowEventsArgs;

            // Create GridCell only for editable columns
            // UseOnlyRendererElement for Non-Editable columns
            if (!UseOnlyRendererElement && cellContainer == null)
            {
                cellContainer = OnPrepareUIElements();
            }

            if (this.SupportsRenderOptimization && !isInEdit)
            {
                if ((gridUnBoundRowEventsArgs != null && !gridUnBoundRowEventsArgs.hasCellTemplate))
                {
                    // Cell Content will be created for Non Template cells.
                    cellcontent = CreateOrRecycleDisplayUIElement();
                    InitializeDisplayElement(dataColumn, (D)cellcontent, record);
                    WireDisplayUIElement((D)cellcontent);
                }
                else
                {
                    // We wont create Cell Content for Templated cells.
                    // GridCell is used as RendererElement with template case.
                    InitializeTemplateElement(dataColumn, (ContentControl)cellContainer, record);
                    WireTemplateUIElement((ContentControl)cellContainer);
                }
                if (cellcontent != null)
                {
                    (cellContainer as GridCell).Content = cellcontent;
                }
            }
            else
            {
                cellcontent = CreateOrEditRecycleUIElement();
                if (dataColumn.GridColumn != null)
                {
                    dataColumn.GridColumn.IsInSuspend = true;
                }
                InitializeEditElement(dataColumn, (E)cellcontent, record);
                if (dataColumn.GridColumn != null)
                {
                    dataColumn.GridColumn.IsInSuspend = false;
                }

                WireEditUIElement((E)cellcontent);
                // GridImageColumn, GridHyperLinkColumn and GridCheckBoxColumn are Non-editable columns.
                //So content created and set to GridCell.
                if (cellcontent != null && cellContainer is GridCell)
                {
                    (cellContainer as GridCell).Content = cellcontent;
                }
            }
            return(UseOnlyRendererElement ? cellcontent : cellContainer);
        }
        /// <summary>
        /// Called when the UnBoundRow cell gets start edited.
        /// </summary>
        /// <param name="cellRowColumnIndex">Current cell Row and Column index</param>
        /// <param name="cellElement">Current UnBoudnRowCell</param>
        /// <param name="column">Current column of the row</param>
        /// <param name="record">DataContext of the row</param>
        /// <returns></returns>
        public override bool BeginEdit(RowColumnIndex cellRowColumnIndex, FrameworkElement cellElement, GridColumn column, object record)
        {
            if (!this.HasCurrentCellState)
            {
                return(false);
            }

            if (this.SupportsRenderOptimization)
            {
                E cellcontent = null;

                if (!UseOnlyRendererElement && cellElement == null)
                {
                    throw new Exception("Cell Element will not be get null for any case");
                }

                var dataColumn = (cellElement as GridCell).ColumnBase;
                GridUnBoundRowEventsArgs gridUnBoundRowEventsArgs = (cellElement as GridCell).ColumnBase.GridUnBoundRowEventsArgs;
                OnUnloadUIElements(dataColumn);

                // Cell content will be null for templated case always.

                if (gridUnBoundRowEventsArgs != null)
                {
                    cellcontent = gridUnBoundRowEventsArgs.hasEditTemplate ? null : CreateOrEditRecycleUIElement();
                }

                if (dataColumn.GridColumn != null)
                {
                    dataColumn.GridColumn.IsInSuspend = true;
                }
                InitializeEditElement(dataColumn, (E)(cellcontent ?? cellElement), record);
                if (dataColumn.GridColumn != null)
                {
                    dataColumn.GridColumn.IsInSuspend = false;
                }

                WireEditUIElement((E)(cellcontent ?? cellElement));

                if (cellcontent != null)
                {
                    (cellElement as GridCell).Content = cellcontent;
                }

                OnEnteredEditMode(dataColumn, cellcontent ?? cellElement);
            }
            else
            {
                OnEnteredEditMode(null, this.CurrentCellRendererElement);
            }

            return(this.IsInEditing);
        }
 /// <summary>
 /// When complete edit, we need to raise query again to provide entered value to customer.
 /// </summary>
 /// <param name="dataColumn">DataColumn which holds GridColumn, RowColumnIndex and GridCell </param>
 /// <param name="currentRendererElement">The UIElement that was loaded in edit mdoe</param>
 /// RowColumnIndex - RowColumnIndex for the Renderer Element
 ///  GridColumn - Column which is providing the information for Binding
 protected override void OnEditingComplete(DataColumnBase dataColumn, FrameworkElement currentRendererElement)
 {
     if (HasCurrentCellState && IsInEditing)
     {
         var data = (CurrentCellRendererElement as TextBox).Text;
         var args = new GridUnBoundRowEventsArgs(dataColumn.GridUnBoundRowEventsArgs.GridUnboundRow, UnBoundActions.CommitData, data, dataColumn.GridColumn, dataColumn.GridUnBoundRowEventsArgs.CellType, this.DataGrid, new RowColumnIndex(dataColumn.RowIndex, dataColumn.ColumnIndex))
         {
             CellTemplate = dataColumn.GridUnBoundRowEventsArgs.CellTemplate,
             EditTemplate = dataColumn.GridUnBoundRowEventsArgs.EditTemplate
         };
         dataColumn.GridUnBoundRowEventsArgs.Value = args.Value;
         DataGrid.RaiseQueryUnBoundRow(args);
     }
     base.OnEditingComplete(dataColumn, currentRendererElement);
 }
 protected override void OnEditingComplete(DataColumnBase dataColumn, FrameworkElement currentRendererElement)
 {
     if (HasCurrentCellState && IsInEditing)
     {
         var dataContextHelper = (CurrentCellRendererElement as ContentControl).Content as DataContextHelper;
         if (dataContextHelper != null)
         {
             var args = new GridUnBoundRowEventsArgs(dataColumn.GridUnBoundRowEventsArgs.GridUnboundRow, UnBoundActions.CommitData, dataContextHelper.Value, dataColumn.GridColumn, dataColumn.GridUnBoundRowEventsArgs.CellType, this, new RowColumnIndex(dataColumn.RowIndex, dataColumn.ColumnIndex))
             {
                 CellTemplate = dataColumn.GridUnBoundRowEventsArgs.CellTemplate,
                 EditTemplate = dataColumn.GridUnBoundRowEventsArgs.EditTemplate
             };
             dataColumn.GridUnBoundRowEventsArgs.Value = args.Value;
             DataGrid.RaiseQueryUnBoundRow(args);
         }
     }
     base.OnEditingComplete(dataColumn, currentRendererElement);
     RaiseCurrentCellValueChangedEvent(DataGrid, dataColumn);
 }
Example #6
0
        // which customize the UnBoundRow values.
        void SfDataGrid_QueryUnBoundRow(object sender, GridUnBoundRowEventsArgs e)
        {
            if (!(totalSales.ContainsKey(e.Column.MappingName)))
            {
                return;
            }

            if (e.UnBoundAction == UnBoundActions.QueryData)
            {
                if (e.RowColumnIndex.RowIndex == 1)
                {
                    if (e.RowColumnIndex.ColumnIndex == 0)
                    {
                        e.Value        = "Total Sales By Month";
                        e.CellTemplate = App.Current.Resources["UnBoundCellTemplate"] as DataTemplate;
                    }
                    else
                    {
                        e.Value        = totalSales[e.Column.MappingName];
                        e.CellTemplate = App.Current.Resources["UnBoundRowCellTemplate"] as DataTemplate;
                    }
                }
                else if (e.GridUnboundRow.UnBoundRowIndex == 0 && e.GridUnboundRow.Position == UnBoundRowsPosition.Bottom && e.GridUnboundRow.ShowBelowSummary == true)
                {
                    if (e.RowColumnIndex.ColumnIndex == 0)
                    {
                        e.Value        = "Percent of Selected Rows";
                        e.CellTemplate = App.Current.Resources["UnBoundCellTemplate"] as DataTemplate;
                    }
                    else
                    {
                        if (!(totalSelectedSales.ContainsKey(e.Column.MappingName)))
                        {
                            return;
                        }

                        e.Value        = totalSelectedSales[e.Column.MappingName] * (percentSales[e.Column.MappingName] / 100);
                        e.CellTemplate = App.Current.Resources["UnBoundRowCellTemplate"] as DataTemplate;
                    }
                }
                else if (e.GridUnboundRow.UnBoundRowIndex == 1 && e.GridUnboundRow.Position == UnBoundRowsPosition.Bottom && e.GridUnboundRow.ShowBelowSummary == true)
                {
                    if (e.RowColumnIndex.ColumnIndex == 0)
                    {
                        e.Value        = "Summary of Selected Rows";
                        e.CellTemplate = App.Current.Resources["UnBoundCellTemplate"] as DataTemplate;
                    }
                    else
                    {
                        if (!(totalSelectedSales.ContainsKey(e.Column.MappingName)))
                        {
                            return;
                        }

                        e.Value        = totalSelectedSales[e.Column.MappingName];
                        e.CellTemplate = App.Current.Resources["UnBoundRowCellTemplate"] as DataTemplate;
                    }
                }

                else if (e.GridUnboundRow.UnBoundRowIndex == 2 && e.GridUnboundRow.Position == UnBoundRowsPosition.Bottom && e.GridUnboundRow.ShowBelowSummary == true)
                {
                    int count = this.SfDataGrid.SelectedItems.Count;
                    if (e.RowColumnIndex.ColumnIndex == 0)
                    {
                        e.Value        = "Average of Selected Rows";
                        e.CellTemplate = App.Current.Resources["UnBoundCellTemplate"] as DataTemplate;
                    }
                    else
                    {
                        if (!(totalSelectedSales.ContainsKey(e.Column.MappingName)))
                        {
                            return;
                        }

                        e.Value        = (totalSelectedSales[e.Column.MappingName] / count);
                        e.Value        = double.IsNaN((double)e.Value) ? 0.0 : e.Value;
                        e.CellTemplate = App.Current.Resources["UnBoundRowCellTemplate"] as DataTemplate;

                        if (e.RowColumnIndex.ColumnIndex > 7)
                        {
                            e.CellTemplate = null;
                        }
                    }
                }
                else if (e.GridUnboundRow.Position == UnBoundRowsPosition.Bottom && e.GridUnboundRow.ShowBelowSummary == false)
                {
                    if (e.RowColumnIndex.ColumnIndex == 0)
                    {
                        e.Value = "Edit the Row to get the Percent of Selected Rows Summary";
                    }
                    else
                    {
                        e.Value        = percentSales[e.Column.MappingName];
                        e.EditTemplate = App.Current.Resources["UnBoundRowEditTemplate"] as DataTemplate;
                        e.CellTemplate = App.Current.Resources["UnBoundRowCellPercentTemplate"] as DataTemplate;
                    }
                }
                e.Handled = true;
            }
            else if (e.UnBoundAction == UnBoundActions.CommitData)
            {
                if (e.Value.ToString().Equals(string.Empty))
                {
                    return;
                }
                double data;
                foreach (char character in e.Value.ToString().ToCharArray())
                {
                    if (char.IsLetter(character))
                    {
                        return;
                    }
                }

                if (e.Value.ToString().Contains("$"))
                {
                    data = Convert.ToDouble(e.Value.ToString().Substring(1, e.Value.ToString().Length - 1));
                }
                else
                {
                    data = Convert.ToDouble(e.Value);
                }
                var value = Convert.ToDouble(data);
                percentSales[e.Column.MappingName] = value;
                e.CellTemplate = App.Current.Resources["UnBoundRowCellTemplate"] as DataTemplate;
            }
        }
        // which customize the UnBoundRow values.
        void SfDataGrid_QueryUnBoundRow(object sender, GridUnBoundRowEventsArgs e) {
            if (!(totalSales.ContainsKey(e.Column.MappingName)))
                return;

            if (e.UnBoundAction == UnBoundActions.QueryData) {
                if (e.RowColumnIndex.RowIndex == 1) {
                    if (e.RowColumnIndex.ColumnIndex == 0) {
                        e.Value = "Total Sales By Month";
                        e.CellTemplate = Application.Current.Resources["UnBoundCellTemplate"] as DataTemplate;
                    }
                    else {
                        e.Value = totalSales[e.Column.MappingName];
                        e.CellTemplate = Application.Current.Resources["UnBoundRowCellTemplate"] as DataTemplate;
                    }
                }
                else if (e.GridUnboundRow.UnBoundRowIndex == 0 && e.GridUnboundRow.Position == UnBoundRowsPosition.Bottom && e.GridUnboundRow.ShowBelowSummary == true) {
                    if (e.RowColumnIndex.ColumnIndex == 0) {
                        e.Value = "Percent of Selected Rows";
                        e.CellTemplate = Application.Current.Resources["UnBoundCellTemplate"] as DataTemplate;
                    }
                    else {
                        if (!(totalSelectedSales.ContainsKey(e.Column.MappingName)))
                            return;

                        e.Value = totalSelectedSales[e.Column.MappingName] * (percentSales[e.Column.MappingName] / 100);
                        e.CellTemplate = Application.Current.Resources["UnBoundRowCellTemplate"] as DataTemplate;
                    }
                }
                else if (e.GridUnboundRow.UnBoundRowIndex == 1 && e.GridUnboundRow.Position == UnBoundRowsPosition.Bottom && e.GridUnboundRow.ShowBelowSummary == true) {
                    if (e.RowColumnIndex.ColumnIndex == 0) {
                        e.Value = "Summary of Selected Rows";
                        e.CellTemplate = Application.Current.Resources["UnBoundCellTemplate"] as DataTemplate;
                    }
                    else {
                        if (!(totalSelectedSales.ContainsKey(e.Column.MappingName)))
                            return;

                        e.Value = totalSelectedSales[e.Column.MappingName];
                        e.CellTemplate = Application.Current.Resources["UnBoundRowCellTemplate"] as DataTemplate;
                    }
                }

                else if (e.GridUnboundRow.UnBoundRowIndex == 2 && e.GridUnboundRow.Position == UnBoundRowsPosition.Bottom && e.GridUnboundRow.ShowBelowSummary == true) {
                    int count = this.SfDataGrid.SelectedItems.Count;
                    if (e.RowColumnIndex.ColumnIndex == 0) {
                        e.Value = "Average of Selected Rows";
                        e.CellTemplate = Application.Current.Resources["UnBoundCellTemplate"] as DataTemplate;
                    }
                    else {
                        if (!(totalSelectedSales.ContainsKey(e.Column.MappingName)))
                            return;

                        // rjl - just to get by divide by zero
                        count = count == 0 ? 1 : count;

                        e.Value = (totalSelectedSales[e.Column.MappingName] / count);
                        e.Value = double.IsNaN(Convert.ToDouble(e.Value)) ? 0.0 : e.Value;
                        e.CellTemplate = Application.Current.Resources["UnBoundRowCellTemplate"] as DataTemplate;

                        if (e.RowColumnIndex.ColumnIndex > 7)
                            e.CellTemplate = null;
                    }
                }
                else if (e.GridUnboundRow.Position == UnBoundRowsPosition.Bottom && e.GridUnboundRow.ShowBelowSummary == false) {
                    if (e.RowColumnIndex.ColumnIndex == 0) {
                        e.Value = "Edit the Row to get the Percent of Selected Rows Summary";
                    }
                    else {
                        e.Value = percentSales[e.Column.MappingName];
                        e.EditTemplate = Application.Current.Resources["UnBoundRowEditTemplate"] as DataTemplate;
                        e.CellTemplate = Application.Current.Resources["UnBoundRowCellPercentTemplate"] as DataTemplate;
                    }
                }
                e.Handled = true;
            }
            else if (e.UnBoundAction == UnBoundActions.CommitData) {
                if (e.Value.ToString().Equals(string.Empty))
                    return;
                double data;
                foreach (char character in e.Value.ToString().ToCharArray())
                    if (char.IsLetter(character))
                        return;

                if (e.Value.ToString().Contains("$"))
                    data = Convert.ToDouble(e.Value.ToString().Substring(1, e.Value.ToString().Length - 1));
                else
                    data = Convert.ToDouble(e.Value);
                var value = Convert.ToDouble(data);
                percentSales[e.Column.MappingName] = Convert.ToDecimal(value);
                e.CellTemplate = Application.Current.Resources["UnBoundRowCellTemplate"] as DataTemplate;
            }
        }