Home Forums WinForms controls Xceed Grid for WinForms StatFunction using more than one column to calculate weighted average.

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • User (Old forums)
    Member
    Post count: 23064
    #17738 |

    Hi,

    I have gone through the basic examples, but I cannot find exactly what I’m looking for.

    I have a Xceed grid, version 3.9.13165.12030, with columns for quantity and price.

    When grouping the grid, I would like to show the weighted average per group, for the (quantity * price)/(rows), instead of just price/rows, in the price column.

    Is it possible to do this, or possible to get this functionality?

    Imported from legacy forums. Posted by Comodo (had 1086 views)

    Diane [Xceed]
    Moderator
    Post count: 1353

    Hi Comodo,

    To achieve this, you will need the SiblingDataRowsChanged of the SummaryRow.

    If you subscribe to that event and update a ValueRow or TextRow, you will get what you need.

    For example:

      {
        m_summaryRow = new SummaryRow();
        grid.FixedFooterRows.Add( m_summaryRow );

        // Value1
        var summaryCell = m_summaryRow.Cells[ 0 ] as SummaryCell;
        summaryCell.StatFunction = StatFunction.Sum;

        // Value2
        summaryCell = m_summaryRow.Cells[ 1 ] as SummaryCell;
        summaryCell.StatFunction = StatFunction.Sum;

        m_textRow  = new TextRow();
        grid.FixedFooterRows.Add( m_textRow );

        this.RefreshTextRow();
        m_summaryRow.SiblingDataRowsChanged += ( s, args ) => this.RefreshTextRow();
      }

      private void RefreshTextRow()
      {
        // Custom Calculation using Value1 and Value2
        var stat = ( ( int )m_summaryRow.Cells[ 0 ].Value + ( int )m_summaryRow.Cells[ 1 ].Value ) / 2;
        m_textRow.Text = stat.ToString();
      }

    If you need to put this value directly in the SummaryRow, then that is more complicated, as you cannot force a value in a SummaryCell, but you can set the TextFormat of the SummaryRow to display your information.

    For example (using our SolidFoundation sample):

      private void InitializeGridControl()
      {
        […]

        var summaryCell = this.summaryRow1.Cells[0] as SummaryCell;
        summaryCell.StatFunction = StatFunction.Count;
        summaryCell.Visible = false;

        summaryCell = this.summaryRow1.Cells[3] as SummaryCell;
        summaryCell.StatFunction = StatFunction.Sum;
        summaryCell.Visible = false;

        this.RefreshSummaryRow();
        this.summaryRow1.SiblingDataRowsChanged += (s, args) => this.RefreshSummaryRow();

        this.gridControl1.EndInit();
      }

      private void RefreshSummaryRow()
      {
        double value1 = double.Parse(this.summaryRow1.Cells[3].Value.ToString());
        double value2 = double.Parse(this.summaryRow1.Cells[0].Value.ToString());

        double result = value1 / value2;

        this.summaryRow1.TextFormat = “Total UnitPrice / Record Count = ” + result.ToString();
      }

    Or alternatively, display all the results in a ValueRow instead:

      private void InitializeGridControl()
      {
        […]

        var summaryCell = this.summaryRow1.Cells[0] as SummaryCell;
        summaryCell.StatFunction = StatFunction.Count;

        summaryCell = this.summaryRow1.Cells[3] as SummaryCell;
        summaryCell.StatFunction = StatFunction.Sum;

        m_valueRow = new ValueRow();
        this.gridControl1.FixedFooterRows.Add(m_valueRow);
        m_valueRow.Cells[2].CellViewerManager = new CellViewerManager();

        this.RefreshValueRow();
        this.summaryRow1.SiblingDataRowsChanged += (s, args) => this.RefreshValueRow();

        this.summaryRow1.Visible = false;
        this.gridControl1.EndInit();
      }

      ValueRow m_valueRow;
      private void RefreshValueRow()
      {
        double value1 = double.Parse(this.summaryRow1.Cells[3].Value.ToString());
        double value2 = double.Parse(this.summaryRow1.Cells[0].Value.ToString());
        double result = value1 / value2;

        m_valueRow.Cells[3].Value = value1.ToString();
        m_valueRow.Cells[0].Value = value2.ToString();
        m_valueRow.Cells[2].Value = result.ToString();
      }

    Imported from legacy forums. Posted by Diane [Xceed] (had 233 views)

    User (Old forums)
    Member
    Post count: 23064

    Thank you Diane.

    I got it working by using the ValueRow to display the results.

    Imported from legacy forums. Posted by Comodo (had 1154 views)

Viewing 3 posts - 1 through 3 (of 3 total)
  • You must be logged in to reply to this topic.